Simplest way to remove trailing zeros of decimal numbers


This is a simple method I found to remove or strip trailing zeros of a decimal number. The function accepts the decimal number and return the trimmed value back. It removes all the redundant zeros in the number after the decimal point and if no number other than zero is present after the decimal point the decimal point too is removed.

function clean_num($num){
                return trim(trim($num, '0'), '.');
        }

The code is written in .php but can be converted to any other language by writing trim function also.


, , ,

  1. #1 by shankar nagvekar on August 5, 2010 - 3:57 pm

    Hi,
    it will fail if the $num =0.01;
    $var = trim(strrev(ltrim(strrev($number), ’0′)),’.');
    try out this one

    function clean_num($num){
    return trim(strrev(ltrim(strrev($number), ’0′)),’.');
    }

  2. #2 by personal software on August 11, 2010 - 5:04 pm

    One again, your articles is very good.thank you!very much.

  3. #3 by Charlie on December 15, 2010 - 2:51 am

    This will also not work if the number is 0.00

    In this case the function returns and empty string.

    Therefore you should use this function:

    function clean_num($num){
    if ($num==0) {return 0;}
    return rtrim(rtrim($num, ’0′), ‘.’); }

    Also it will only strip trailing zeros, if you also want to strip leading zeros, use trim in place of rtrim.

  4. #4 by Sasa on March 18, 2011 - 9:50 pm

    I don’t know php very well, so I don’t understand how decimal number can have leading or trailing zeros. In that case, that is not decimal number. Suppose that $num is string, try this code

    function clean_num($num){
    return (string)(floatval($num));
    }

  5. #5 by Sergiu on April 12, 2011 - 7:20 pm

    I tried what you all said, but it didn’t work…

    here is what worked :D
    function clean_num($number) {
    return trim(strrev(ltrim(strrev($number), 0)),”.”);
    }

  6. #6 by Sumesh on May 31, 2011 - 3:34 pm

    Please dont use this function it trims useful zeros. For eg. try clean_num(80). It will print 8!!!

  7. #7 by bob on September 13, 2011 - 3:52 am

    Sumesh – you are correct – but that wasn’t the purpose of the function. It is to be used on database fields that are of type decimal. This function works perfectly.

  8. #8 by web-developer on October 3, 2011 - 2:49 pm

    You should use this function, it works good with decimal (including 0.01 and 0.000) and integer (including 80):
    [code]
    function clean_num( $num ){
    $pos = strpos($num, '.');
    if($pos === false) { // it is integer number
    return $num;
    }else{ // it is decimal number
    return rtrim(rtrim($num, '0'), '.');
    }
    }
    [/code]

  9. #9 by max4ever on February 2, 2012 - 8:28 pm

    echo (float) “0.123000″; // ==> 0.123

  10. #10 by rami on August 15, 2012 - 6:37 am

    $num = “5.6040000″;
    (float)$num

    will result:
    5.604

  11. #11 by mohrt on January 22, 2013 - 8:14 am

    return (strpos($num,’.')===false) ? $num : rtrim(rtrim($num, ’0′), ‘.’);

  12. #12 by royalty free music reviews on December 8, 2013 - 10:17 am

    Greetings! I’ve been reading your web site for a long
    time now and finally got the bravery to go ahead and give you a shout out from Atascocita Tx!
    Just wanted to mention keep up the excellent work!

(will not be published)