Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How do I round down to two decimal places?

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.

like image 837
Adam Moss Avatar asked Sep 05 '12 09:09

Adam Moss


People also ask

How do you round to 2 decimal places in PHP?

The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function.

How do you round down in PHP?

The floor() function rounds a number DOWN to the nearest integer, if necessary, and returns the result. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a floating-point number, look at the round() function.

How do you round something to 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How do you round down a decimal?

There are certain rules to follow when rounding a decimal number. Put simply, if the last digit is less than 5, round the previous digit down. However, if it's 5 or more than you should round the previous digit up. So, if the number you are about to round is followed by 5, 6, 7, 8, 9 round the number up.


2 Answers

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

like image 109
GeoffreyB Avatar answered Sep 19 '22 17:09

GeoffreyB


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 < 0) { $precision = 0; }     $numPointPosition = intval(strpos($val, '.'));     if ($numPointPosition === 0) { //$val is an integer         return $val;     }     return floatval(substr($val, 0, $numPointPosition + $precision + 1)); } 

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

like image 36
Alex Avatar answered Sep 17 '22 17:09

Alex