I store money values in my db table. E.g. I have 2.50. But when I print that value the 0 is always missing so I get 2.5. The db table money field has the following type: decimal(6,2)
any idea how to fix that?
By default PHP echo and print don't print the trailing zeros of a floating point number.
To overcome this you need to use printf as:
$money = 2.50;
printf("%.2f",$money); // prints 2.50
echo $money; // prints 2.5
print $money; // prints 2.5
The format specifier used in printf is "%.2f" what it means is always print atleast two digits after the decimal point and if there are not so many digits use 0. Note that it is atleast two digits not equal to two digits. So if I print 1.234 with that format it will not truncate it to 1.23 but will print 1.234 and if I print 1 it will result in 1.00
Related
I am trying many typed of fields in MySQL to store numbers only, tried with INT, BIGINT with leading zeros, CHAR and VARCHAR to store INVOICE NUMBERS
I need the invoice numbers to be start with 0000000001, I stored it manually in PHPmyadmin
Now I want to display it and I dont get the leading zeros ....
Here is the database
field "folio" CHAR 15 stored I have manually did 0000000001 it displays fine on phpmyadmin
but here is the problem
<?php $maxprod=mysqli_query($datacenter,
"SELECT * FROM ventas
WHERE documento = 'boleta'
ORDER BY id DESC LIMIT 1");
while($lastcode=mysqli_fetch_assoc($maxprod)){?>
<input type="text" value="<?php echo $lastcode['folio']+1?>">
<?php }?>
the result of the query is 1 just 1 it does not display all other zeros
Any idea why?
PHP automatically converts string into number if you are performing any numerical operation on it. But you can keep the order number in integer form and pad it with zeroes when necessary:
str_pad($lastcode['folio']+1, 15, "0", STR_PAD_LEFT);
echo $lastcode['folio'] should show you a result with the leading zeroes, but not $lastcode['folio']+1.
As soon as you do that +1, the result is no longer a string. The $lastcode['folio'] variable is converted to a number in order to do the arithmetic operation on it.
The leading zeroes are just formatting and don't need to be stored with the number. If you need an autoincrementing number, just use an autoincrement integer in MySQL, and format the number with leading zeroes when you print it out.
This will retrieve the number as a string to display it as text.
Later you can manipulate it on your coding environment.
SELECT CAST(document_number AS CHAR) FROM ...
OK so in the DB table I have 2 cols & values
Lets say
open = 1.23450 & close = 1.23400
now here is the PHP code that I am trying to use for the calculation
$pips = abs($value['close'] - $value['open'])*1;
This should return a value of -0.0005
However it just simply returns value 0 when I echo $pips
If number of digits after the decimal are fixed ( say 5 digits precision)
$pips = abs($value['close']*100000 - $value['open']*100000);
$pips = $pips/100000;
You are using abs() which returns absolute positive value of a number so you need to multiply by "-1" (minus) and as suggested floating points aren't so good in PHP so better calculate the $pips as a whole number first then dividing depending on the number of digits after the point.
I have two, hopefully basic number questions.
First, I am using the decimal data type on MySQL to show data like speed or height set to one decimal place like this decimal(4,1) My problem is that for numbers with no dp it is still showing .0 - What I get is '309.0' but what I want is '309'. What do I need to change to hide the '.0'?
Second I have an integer data type showing fixed numbers. Many of the numbers are 0 zero but these are getting treated as null and not displaying. How do I force the 0's to display? (I can't make table changes for this because other columns do have zero's that are null values. It is only this one column that needs zeros to display).
For both of these problems PHP is being used to display the results.
EDIT: code I'm using to display results. the top one needs to not show .0 the bottom one needs to show a zero.
Length:</b> %sft' . PHP_EOL, $row2['length'])
Inversions:</b> %s' . PHP_EOL, $row2['inversions'])
I believe a sneaky trick is if you add 0 to the end of your number, it removes the .0000 etc.
example:
$number = 150.00
$number = $number + 0; //should echo 150
$other_number = 150.500;
$other_number += 0; //should echo 150.5
As for the other, you can simply concatenate the zeros since no calculations seem to be needed
To check for the trailing zero, you could use the following to make sure that the float and integer values are the same:
$num = (floatval($num)==intval($num)) ? intval($num) : floatval($num);
and then for the null value, you could use coalesce(yourColumn, 0) as yourColumn to either get your column value if not null or return 0 as the value..
I made this simple function, and the result returns 1 rather than 0.5
What did I do wrong?
DELIMITER //
DROP FUNCTION IF EXISTS test_decimal //
CREATE FUNCTION test_decimal(input DECIMAL)
RETURNS DECIMAL
BEGIN
SET #_credit = 0.5;
RETURN input * #_credit;
END //
DELIMITER ;
SELECT test_decimal(1);
Because you didn't specify the precision and scale that's why it's rounding the value. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point. By default, the value of the precision is 10 and the value of scale is 0. So, RETURNS DECIMAL is the same as RETURNS DECIMAL(10,0). If the scale is 0, DECIMAL values contain no decimal point or fractional part. Try specifying in to your function.
RETURNS DECIMAL(5,2) -- 999.99
DECIMAL, NUMERIC
SQLFiddle Sample
I need to format a floating point number in PHP so it can be inserted into a column in a table which is of type decimal(6,5).
So,
20 would get formatter to 20.00000
20.5 would get formatted to 20.50000
How to do it in PHP?
To avoid the float inherent precision error,
cast first to decimal(9,2), then to decimal(6,5).
To format a number just use
<?php
echo number_format(20, 5); //returns 20.00000
echo number_format(20.5, 5); //returns 20.50000
?>
But you don't need to do that to insert into a MySQL DB.