-1

I am new to php. Can anyone tell how to change the query such that it will print 16.66. I need 16.66667 to be only 16.66.

    <?php
    $number = 16.6666667;
    $n = number_format($number, 2);
    echo $n; // 16.67    
?>

I am getting output as 16.67. Thanks for any help.

ayush
  • 14,350
  • 11
  • 53
  • 100
Sandeep V
  • 463
  • 1
  • 11
  • 20
  • check this http://stackoverflow.com/questions/9944001/delete-digits-after-two-decimal-point-not-round-number-in-php – andrew Jan 29 '14 at 07:01

2 Answers2

1

Use the following code.

$number=floor(16.6666667 * 100) / 100;

This will return value without rounding the number.

Jenz
  • 8,280
  • 7
  • 44
  • 77
0
$n = substr($number, 0, strrpos($number, '.') + 3);

EDIT

For any number with or without decimals, try this:

$n = number_format($number, 3);
$n = substr($n, 0, strrpos($n, '.') + 3);
evalarezo
  • 1,134
  • 7
  • 13