Delete digits after two decimal point not round number using PHP - php

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!

Related

Using PHP floatval adds undesired trailing numbers behind decimal places

I am having a problem with php conversion from string to float
In some cases, if I do:
floatval("8.80")
I get:
8.800000000000001
I have struggled with round(x,1), number_format, etc, but to no avail
What am I getting wrong here?
By using number_format will help to resolve your issue
<?php
$number = 8.800000000000001;
$precision = 1;
$number = intval($number * ($p = pow(10, $precision))) / $p;
echo number_format((float) $number, $precision, '.', '');
?>

Formatting int with decimals PHP

I'm have this number
55550
But I it should be 555.50, I tried number_format() and sprinft but did not work.
<?php
$num = 55550;
$num = (float) $num / 100;
echo number_format($num, 2, ".", "");
by casting the number to float, and dividing by 100 you can use number_format to change the format.

Unexpected result for number_format and sprintf when trying to display number with two decimals

I have a calculation that returns this double: 6.4971508379888. If I cast it to a float and echo it, it's the same number. I want to display (not round) the number with only two decimals, so I tried:
number_format((float)$number, 2, '.', '')
and
sprintf('%0.2f', (float)$number)
but in both cases I see 6.50 instead of 6.49. Why is this happening?
PHP automatically round the value based on given precision
If you want your expected results then follow the following code.
$precision = 2;
$number = floor($number * pow(10,$precision))/pow(10,$precision);
echo number_format((float)$number, $precision, '.', '');
This is a slight modification to the accepted answer because the solution provided wasn't giving an accurate result. It was causing the last digit to get rounded (down).
$digits = 2;
$number = floor($number * pow(10, ($digits + 1)) / pow(10, ($digits + 1));
echo number_format($number, $digits, '.', '');
Also, there should be no need to type-cast to a float when passing the number to number_format(). The calculation (and the function itself) will automatically set the type accordingly.

Set precision for a float number in PHP

I get a number from database and this number might be either float or int.
I need to set the decimal precision of the number to 3, which makes the number not longer than (regarding decimals) 5.020 or 1518845.756.
Using PHP
round($number, $precision)
I see a problem:
It rounds the number. I need a function to only cut the decimals short, without changing their values which round( ) seems not to follow.
You can use number_format() to achieve this:
echo number_format((float) $number, $precision, '.', '');
This would convert 1518845.756789 to 1518845.757.
But if you just want to cut off the number of decimal places short to 3, and not round, then you can do the following:
$number = intval($number * ($p = pow(10, $precision))) / $p;
It may look intimidating at first, but the concept is really simple. You have a number, you multiply it by 103 (it becomes 1518845756.789), cast it to an integer so everything after the 3 decimal places is removed (becomes 1518845756), and then divide the result by 103 (becomes 1518845.756).
Demo
Its sound like floor with decimals. So you can try something like
floor($number*1000)/1000
If I understand correctly, you would not want rounding to occur and you would want the precision to be 3.
So the idea is to use number_format() for a precision of 4 and then remove the last digit:
$number = '1518845.756789';
$precision = 3;
echo substr(number_format($number, $precision+1, '.', ''), 0, -1);
Will display:
1518845.756
rather than:
1518845.757
Links : number_format() , substr()
See this answer for more details.
function numberPrecision($number, $decimals = 0)
{
$negation = ($number < 0) ? (-1) : 1;
$coefficient = pow(10, $decimals);
return $negation * floor((string)(abs($number) * $coefficient)) / $coefficient;
}
$num=5.1239;
$testnum=intval($num*1000)/1000;
echo $testnum; //return 5.123

Printing PHP float with 2 digits after the decimal point?

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

Categories