-2
<?php 
    var $a = 85;
    var $b = 7;
    var $c = $a/$b;
    echo $c;
?>

Answer is 12.14285714285714

I want to fixed it as 12.14. How can we set two digits after decimal in PHP?

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37

2 Answers2

7

PHP doc : PHP Doc to round numbers

In your example :

var $b = 7;

var $c = $a/$b;

echo round($c, 2);

Hope it helps

  • It should probably be `floor()` and not round(). Round will make 87.555 -> 87.56, Floor() makes it 87.55 as requested in the question. But who knows, it may be a typo from OP. – Andreas Jul 02 '17 at 18:06
  • Yes my bad. Anyway there is some function and PHP Documentation is full of it with examples :p – Laurent Plantrose Jul 02 '17 at 18:17
2

You can use number_format:

number_format($c, 2)
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175