PHP How do I round down to two decimal places?

匿名 (未验证) 提交于 2019-12-03 02:28:01

问题:

I need to round down a decimal in PHP to two decimal places so that:

49.955 

becomes...

49.95 

I have tried number_format, but this just rounds the value to 49.96. I cannot use substr because the number may be smaller (such as 7.950). I've been unable to find an answer to this so far.

Any help much appreciated.

回答1:

This can work: floor($number * 100) / 100



回答2:

Here is a nice function that does the trick without using string functions:

An other option is to subtract a fraction before rounding:

function floorp($val, $precision) {     $half = 0.5 / pow(10, $precision);     return round($val - $half, $precision); } 


回答3:

Unfortunately, none of the previous answers (including the accepted one) works for all possible inputs.

1) sprintf('%1.'.$precision.'f', $val)

Fails with a precision of 2 : 14.239 should return 14.23 (but in this case returns 14.24).

2) floatval(substr($val, 0, strpos($val, '.') + $precision + 1))

Fails with a precision of 0 : 14 should return 14 (but in this case returns 1)

3) substr($val, 0, strrpos($val, '.', 0) + (1 + $precision))

Fails with a precision of 0 : -1 should return -1 (but in this case returns '-')

4) floor($val * pow(10, $precision)) / pow(10, $precision)

Although I used this one extensively, I recently discovered a flaw in it ; it fails for some values too. With a precision of 2 : 2.05 should return 2.05 (but in this case returns 2.04 !!)

So far the only way to pass all my tests is unfortunately to use string manipulation. My solution based on rationalboss one, is :

function floorDec($val, $precision = 2) {     if ($precision 

This function works with positive and negative numbers, as well as any precision needed.

You can find a working example here : http://codepad.viper-7.com/ZGprQJ.



回答4:

Multiply your input by 100, floor() it, then divide the result by 100.



回答5:

Try the round() function

Like this: round($num, 2, PHP_ROUND_HALF_DOWN);



回答6:

    function roundDown($decimal, $precision)     {         $fraction = substr($decimal - floor($decimal), 2, $precision); //calculates the decimal places to $precision length         $newDecimal = floor($decimal). '.' .$fraction; // reconstructs decimal with new # of decimal places          return floatval($newDecimal);     }      echo roundDown(345.8768378, 5); // OUTPUT: 345.87683 

This function works with positive and negative numbers.

Code example here: http://codepad.org/RAOyBCuY



回答7:

Use formatted output

sprintf("%1.2f",49.955) //49.95 

DEMO



回答8:

You can use:

$num = 49.9555; echo substr($num, 0, strpos($num, '.') + 3); 


回答9:

An alternative solution using regex which should work for all positive or negative numbers, whole or with decimals:

if (preg_match('/^-?(\d+\.?\d{1,2})\d*$/', $originalValue, $matches)){     $roundedValue = $matches[1]; } else {     throw new \Exception('Cannot round down properly '.$originalValue.' to two decimal places'); } 


回答10:

If you want to round the decimal down only when the last part of it is 5 or bellow then the best solution is to use:

round(4.955,2,PHP_ROUND_HALF_DOWN); 

Now, if you want to always round the decimals down and still have control of the amount of decimals, best solution is:

round(floor(4.957*100)/100,2) 

Also consider that the "100" number must scale togheter with the "2" decimal. E.G. If you want 4 decimals you will have to multiply by 10000 and divide by 10000 so you will only lose the numbers beyond that point.



回答11:

What about this?

$value = 49.955;  echo intval( $value * 100 ) / 100; 

Here is a demo



回答12:

sprintf("%1.2f",49.955) //49.95 

if you need to truncate decimals without rounding - this is not suitable, because it will work correctly until 49.955 at the end, if number is more eg 49.957 it will round to 49.96
It seems for me that Lght`s answer with floor is most universal.



回答13:

Did you try round($val,2) ?

More information about the round() function



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!