1

Need to round 30.61 to 30.60, Any built-in function for PHP to do this ?

Qirel
  • 25,449
  • 7
  • 45
  • 62
Mehar
  • 2,158
  • 3
  • 23
  • 46
  • So you want to round `30.69` to `30.70`? There's no built in function that I'm aware of, but should be quite simple. – Qirel Jan 28 '19 at 06:41
  • 1
    This might help you: https://stackoverflow.com/questions/14903379/rounding-to-nearest-fraction-half-quarter-etc – GasKa Jan 28 '19 at 06:45
  • Possible duplicate of [Rounding to nearest fraction (half, quarter, etc.)](https://stackoverflow.com/questions/14903379/rounding-to-nearest-fraction-half-quarter-etc) – jpeg Jan 28 '19 at 07:06
  • Possible duplicate of [Show a number to 2 decimal places](https://stackoverflow.com/questions/4483540/show-a-number-to-2-decimal-places) – gehbiszumeis Jan 28 '19 at 07:10

2 Answers2

2

If I understand your desired output correctly, that you only want to round the second decimal point, you can round with 1 decimal presicion, then use numer_format() to ensure you get the correct number of decimals.

$num = 30.61;
echo number_format(round($num, 1), 2);
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

you can do this

 $num = 3.61;
 /*round to nearest decimal place*/
 $test_number = round($num,1);
 /* ans :3.6
 format to 2 decimal place*/
 $test_number = sprintf ("%.2f", $test_number);
 /* ans : 3.60 */
Pritamkumar
  • 682
  • 1
  • 11
  • 30