Delete digits after two decimal points, without rounding the value

后端 未结 15 1423
死守一世寂寞
死守一世寂寞 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:36
    $num = 118.74999669307;
    $cut = substr($num, 0, ((strpos($num, '.')+1)+2));
    // Cut the string from first character to a length of 2 past the decimal.
    // substr(cut what, start, ( (find position of decimal)+decimal itself)+spaces after decimal) )
    echo $cut; 
    
    0 讨论(0)
  • 2020-11-29 01:39

    try with number_format:

    echo number_format('2.50000050', 2); // 2.50
    
    0 讨论(0)
  • 2020-11-29 01:42

    A simple function to follow would be "If greater than 0 floor, else ceil", using a multiplier to raise it above the decimal point temporarily whilst doing it:

    function trim_num($num_in, $dec_places = 2) {
        $multiplier = pow(10, $dec_places); // 10, 100, 1000, etc
        if ($num_in > 0) {
            $num_out = floor($num_in * $multiplier) / $multiplier;
        } else {
            $num_out = ceil($num_in * $multiplier) / $multiplier;
        }
        return $num_out;
    }
    
    0 讨论(0)
  • 2020-11-29 01:44

    TL;DR:

    The PHP native function bcdiv seems to do precisely what is required, and properly.

    To simply "truncate" a number, bcdiv($var, 1, 2); where 2 is the number of decimals to preserve (and 1 is the denomenator - dividing the number by 1 allows you to simply truncate the original number to the desired decimal places)

    Full Answer (for history)

    This turns out to be more elusive than one might think.

    After this answer was (incorrectly) upvoted quite a bit, it has come to my attention that even sprintf will round.

    Rather than delete this answer, I'm turning it into a more robust explanation / discussion of each proposed solution.

    number_format - Incorrect. (rounds)
    Try using number format:

    $var = number_format($var, 2, '.', '');  // Last two parameters are optional
    echo $var;
    // Outputs 2.50
    

    If you want it to be a number, then simply type-cast to a float:

    $var = (float)number_format($var, 2, '.', '');
    

    Note: as has been pointed out in the comments, this does in fact round the number.

    sprintf - incorrect. (sprintf also rounds)
    If not rounding the number is important, then per the answer below, use sprintf:

    $var = sprintf("%01.2f", $var);
    

    floor - not quite! (floor rounds negative numbers)

    floor, with some math, will come close to doing what you want:

    floor(2.56789 * 100) / 100; // 2.56
    

    Where 100 represents the precision you want. If you wanted it to three digits, then:

    floor(2.56789 * 1000) / 1000; // 2.567
    

    However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:

    floor(-2.56789 * 100) / 100; // -2.57
    

    "Old" Correct answer: function utilizing floor

    So a fully robust solution requires a function:

    function truncate_number( $number, $precision = 2) {
        // Zero causes issues, and no need to truncate
        if ( 0 == (int)$number ) {
            return $number;
        }
        // Are we negative?
        $negative = $number / abs($number);
        // Cast the number to a positive to solve rounding
        $number = abs($number);
        // Calculate precision number for dividing / multiplying
        $precision = pow(10, $precision);
        // Run the math, re-applying the negative value to ensure returns correctly negative / positive
        return floor( $number * $precision ) / $precision * $negative;
    }
    

    Results from the above function:

    echo truncate_number(2.56789, 1); // 2.5
    echo truncate_number(2.56789);    // 2.56
    echo truncate_number(2.56789, 3); // 2.567
    
    echo truncate_number(-2.56789, 1); // -2.5
    echo truncate_number(-2.56789);    // -2.56
    echo truncate_number(-2.56789, 3); // -2.567
    

    New Correct Answer

    Use the PHP native function bcdiv

    echo bcdiv(2.56789, 1, 1);  // 2.5
    echo bcdiv(2.56789, 1, 2);  // 2.56
    echo bcdiv(2.56789, 1, 3);  // 2.567
    echo bcdiv(-2.56789, 1, 1); // -2.5
    echo bcdiv(-2.56789, 1, 2); // -2.56
    echo bcdiv(-2.56789, 1, 3); // -2.567
    
    0 讨论(0)
  • 2020-11-29 01:45

    Use the PHP native function bcdiv.

    Example:

    echo bcdiv(3.22871, 1, 1);  // 3.2
    echo bcdiv(3.22871, 1, 2);  // 3.22
    echo bcdiv(3.22871, 1, 3);  // 3.228
    echo bcdiv(-3.22871, 1, 1); // -3.2
    echo bcdiv(-3.22871, 1, 2); // -3.22
    

    For your case:

    $var='2.500000550';
    echo $var
    echo bcdiv($var, 1, 2);  // 2.50
    
    0 讨论(0)
  • 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);
    
    }
    
    0 讨论(0)
提交回复
热议问题