$price = 10.00;
list($dollars, $cents) = explode('.', $price);
echo $dollars . '.' . $cents;
... almost works except that the zeros are omitted. 10.00 becomes 10 and 10.10 becomes 10.1
I see there's a padding function for strings, but anything for numbers or floats?
How do I fix this?
You can use number_format:
echo number_format($price, 2); // Would print 10.00
You can specify a separator for the decimal point and another one for the thousands:
echo number_format(1234.56, 2, ',', ' '); // Would print 1 234,56
Use Sprintf
$digit = sprintf("%02d", $digit);
For more information, refer to the documentation of sprintf.
number_format is what you want: http://php.net/manual/en/function.number-format.php
Though i would recommend number_format, you could use
sprintf('%02.2f', $price)
if you want to rely on string functions.
Related
The number is 13911392101301011 and regardless of using sprintf or number_format i get the same strange result.
sprintf('%017.0f', "13911392101301011"); // Result is 13911392101301012
number_format(13911392101301011, 0, '', ''); // Result is 13911392101301012
sprintf('%017.0f', "13911392101301013"); // Result is 13911392101301012
number_format(13911392101301013, 0, '', ''); // Result is 13911392101301012
As you actually have the number as a string, use the %s modifier:
sprintf('%s', "13911392101301011"); // 13911392101301011
Note that PHP is using a signed integer internally. The size depends on your system.
32bit system:
2^(32-1) = 2147483648
64bit system:
2^(64-1) = 9223372036854775808
-1 because 1 bit is reserved for the signage flag.
Since you are dealing with large numbers here, you may want to keep them as strings and perform numerical operation on the string values using BCMath functions.
$val = "13911392101301011";
echo $val; // 13911392101301011
echo bcadd($val, '4'); // 13911392101301015
echo bcmul($val, '2'); // 27822784202602022
You can do easily this way :-
ini_set("precision",25); // change 25 to whatever number you want or need
$num = 13911392101301011;
print $num;
Documentation states that $number in number_format is float so there is explicit typecast. Equivalent would look like this:
sprintf('%017.0f', (float) "13911392101301011");
Float is precise to around 14 digits and your number has 17 digits.
Your number_format call is setting the . and , to blank
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
try this:
number_format(13911392101301011, 0, '.', ',');
Is there an easy way to echo a float number with a specific amount of digits after the decimal point?
For example: $sum = 3.1234566768; I would like to echo $sum and get: 3.12.
use number_format()
number_format($sum,2);
Try with:
$sum = 3.1234566768;
$rounded = round($sum, 2);
echo number_format($sum, 2); // 3.12
echo number_format((float)$ans, 4, '.', '');
I think this will work out
At the moment I store prices for products in the database as a pence number. So 4321 in the database means £43.21.
Then when reading it out, I divide by 100 to get it in pound and pence format.
However, I have a problem.
If the price is 4320, the returned value is 43.2 without the 0.
How can I get around this?
Thanks!
You can format strings with sprintf
See example 9:
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
<?
echo money_format("%i", 1234.5)
//Output: 1234.50
?>
You can use money_format.
money_format() should do the trick. Alternatively number_format() or the powerful printf().
echo number_format($float, 2, '.', '');
and for pretty printing of large values:
echo number_format($float, 2, '.', ',');
I want to add .00 to my value.
For example:
100 will be 100.00
100.26 will be 100.26 only.
$YOUR_VALUE = 1000.25;
echo number_format($YOUR_VALUE, 2);
number_format() can be your friend
Like #Gaurav said, use the number_format() function. Simply pass it the value and the number of digits you want there to be after the decimal point:
$value = 100;
echo number_format($value, 2); //prints "100.00"
Note that by default, it will also insert commas as the thousands separator:
$value = 2013;
echo number_format($value, 2); //prints "2,013.00"
You can change the characters that are used as the decimal point and thousands separator by passing them in as the third and fourth parameters to the function:
$value = 2013;
echo number_format($value, 2, ',', ' '); //prints "2 013,00"
number_format(100, 2, '.', ' ')
you can use
round()
or
number_format()
I have these value stored in a decimal 10,2 field
1052730
956700
How do i print this using php so that the value is like
$10,527.30
$9,567.00
basically i am trying to avoid the value as
$1,052,730 <--- this i dont want
You can use the
money_format($format, $value)
function in php. The details of the formatting is given here.
Well, assuming that 1052730 is really 10527.30 as alluded to in your question:
$number = 1052730;
$decimals = $number % 100; //30 in this case
$digits = floor($number / 100);
$paddedDecimals = str_pad($digits, 2, '0', STR_PAD_LEFT);
$out = '$' . number_format($digits, 0).'.'.$paddedDecimals;
echo $out; // $10,527.30
There are no floating point calculations used for the decimal part, so there's no need to worry about precision issues (although at this precision it would likely be hard to get a float error in there)...
Just divide by 100:
<?php
echo number_format(1052730/100, 2, '.', ',') . PHP_EOL;
echo number_format(956700/100, 2, '.', ',') . PHP_EOL;
printf ("$%01.2f", ($input / 100));