Domanda

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.

È stato utile?

Soluzione 2

$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);

Altri suggerimenti

Use the following code.

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

This will return value without rounding the number.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top