문제

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.

도움이 되었습니까?

해결책 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);

다른 팁

Use the following code.

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

This will return value without rounding the number.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top