Delete digits after two decimal points, without rounding the value

后端 未结 15 1544
死守一世寂寞
死守一世寂寞 2020-11-29 01:04

i have value in php variable like that

$var=\'2.500000550\';
echo $var

what i want is to delete all decimal points after 2 digits.

15条回答
  •  情深已故
    2020-11-29 01:46

    All of the solutions which use number_format are wrong because number_format performs rounding.

    The function below should work on all numbers, you can specify the decimal separator for those countries which use ','.

    function truncate_decimal($number, $truncate_decimal_length = 2, $decimal_character = '.', $thousands_character = '') {
    
    $number = explode($decimal_character, $number);
    $number[1] = substr($number[1], 0, $truncate_decimal_length);
    $number_truncated = implode($decimal_character, $number);
    return number_format($number_truncated, $truncate_decimal_length, $decimal_character, $thousands_character);
    
    }
    

提交回复
热议问题