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
Related
I have a number like this 18914439.4524345860
and I want to display that number like this 18.91
I have try the number_format function
$x = 18914439.4524345860;
echo number_format($x, 2, ".", ".");
and the result is
18.914.439.45
how to show the number like this 18.91?
Thanks for your attention...
hope this help you :
$int = 18914439.4524345860/1000000;
echo round($int,2);
/* Output 18.91*/
18914439.4524345860 is a complete different number than 18.91, not just another representation.
You could divide by 1000000 first, and then use number_format:
$x = 18914439.4524345860;
echo number_format($x / 1000000, 2, ".", ".");
$val = 95454545.455
How to round $val so the result will be 95454546?
Because using round() only return 95454545
EDIT
ceil() is almost correct, but if $val = 95454545.444, I want the function return to 95454545
Regards,
Elmer
Try this
ceil — Round fractions up, Returns the next highest integer value by rounding up value if necessary.
<?php
$val = 95454545.455;
$result = ceil($val);
echo 'Result '.$result;
?>
Use ceil() instead which will round to the next big value,
echo ceil($val);
UPDATE
You can acheive more precision with round() and HALF UP flag,
echo round(95454545.544, 0, PHP_ROUND_HALF_UP); //95454546
echo round(95454545.44, 0, PHP_ROUND_HALF_UP); //95454545
Try this number_format it will round up decimal numbers
<?php
$val = 95454545.455;
$result = number_format($val , 3, '.', ',');
echo 'Result '.$result;
<?
I think, I've found the formula for this kind of problem.
ex:
$var = 95454545.454
I only have to add 0.055 value in it.
$new_var = $var + 0.055 -> $new_var = 95454545.509
$result = round($new_var) -> $result = 95454546
My number: 53.199999999999996
I have tried all of these:
sprintf("%01.2f", $number); // 53.20
sprintf('%0.2f', $number); // 53.20
floor(($number * 100)) / 100; // 53.2
intval(($number * 100)) / 100; // 53.2
I want: 53.19
How can I do that?
You can use number_format(): http://php.net/number_format
$number = 53.1999999
echo int() number_format((float)$number, 2, '.', '');
//53.19
You need floor() in this way:
echo floor($number*100)/100;
Or you cast to integer:
echo 0.01 * (int)($number*100);
This way it will not be rounding up.
I have checked and using intval its works on php version 5.3, however you have mentioned its not worked at your end.
$number = 53.1999999;
echo intval(($number*100))/100;
you must set precision (floating point) before set this float number
ini_set('precision', 17);
$number = 53.199999999999996;
$number = ($pos = strpos($number,'.')) ? substr($number,0,$pos + 3) : number_format($number);
echo $number ;
output :
53.19
Link
PHP round float numbers with this setting, if you set variable without quoting that like :
$number = 53.199999999999996;
echo $number;
PHP will round this number
after var_dump , print ,echo , ... output will be :
53.2
our you can set number value with quoting
$number = '53.199999999999996';
echo $number;
PHP do not round this because it's string now!
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));