-5

Example:

22.453667 => 22.45

34.65355 => 34.65

Brandon
  • 16,382
  • 12
  • 55
  • 88
vino
  • 302
  • 4
  • 18

4 Answers4

2

You should use PHP's round function:

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
?>
Brandon
  • 16,382
  • 12
  • 55
  • 88
vino
  • 302
  • 4
  • 18
  • Any options : echo round(3,4453); // 3,44 echo round(3,536); // 3,53 echo round(3,6783); // 3,37 – vino Mar 13 '14 at 18:38
1

use number_format:

$num = "22.469667";

// Without round
echo number_format( floor($num*100) / 100, 2, '.', '' );
// output: 22.46

echo "\n"; 

// With round
echo number_format($num, 2, '.', '' );
// output: 22.47

http://codepad.org/BjGiEeVa

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
1

use round function

echo round(2.98684, 2); The output is 2.99

codelover
  • 317
  • 1
  • 11
0

number_format() is a lot easier than jogesh_pi made it out to be.

Just do:

$num = "22.453667";
echo number_format($num, 2);

which returns: 22.45

It just needs the number as the first parameter and then the number of decimal points you want it to round to.

$num = "22.457667"; would return 22.46 $num = "22"; would return 22.00

http://codepad.org/9zollLf5

iMakeWebsites
  • 587
  • 3
  • 10