PHP number: decimal point visible only if needed

40,145

Solution 1

I actually think that your workaround is as good as any. It's simple and clear, and there's really no point talking about performance here, so just go for it.

Solution 2

floatval or simply casting to float

php > echo floatval(7.00);
7
php > echo floatval(2.30);
2.3
php > echo floatval(1.25);
1.25
php > echo floatval(1.125);
1.125

php > echo (float) 7.00;
7
php > echo (float) 2.30;
2.3
php > echo (float) 1.25;
1.25
php > echo (float) 1.125;
1.125

Solution 3

As Emil says yours are good. But if you want to remove 0 from e.g. 7.50 too, I've got a suggestion, rtrim():

<?php
    // if $sql_result["col_number"] == 1,455.50
    rtrim(rtrim(number_format($sql_result["col_number"], 2, ".", ""), '0'), '.');
    // will return 1455.5
?>

Solution 4

You could also use rtrim(), which would remove excess 0s, in the case where you might want to keep one decimal place but not the excess zeros. (For example, 4.50 becomes 4.5.) Also allows you to change the number of decimal places from 2 to any other number.

rtrim(rtrim((string)number_format($value, 2, ".", ""),"0"),".");

// 4.00 -> 4
// 4.50 -> 4.5
// 4.54000000 -> 4.54 (if you're doing more decimal places)

Solution 5

Actually I think the cleanest way I can think of to do this for someone that just did a search looking for this sort of thing is to do this:

( number_format ($sql_result["col_number"], 2) * 100 ) / 100;
Share:
40,145
vitto
Author by

vitto

I'm a UX/UI developer, located in Italy

Updated on July 05, 2022

Comments

  • vitto
    vitto almost 2 years

    I'd like to know if exists some function to automatically format a number by it's decimal, so if I have:

    <?php
        // $sql_result["col_number"] == 1,455.75
        number_format ($sql_result["col_number"], 2, ".", "");
        // will return 1455.75
    
        // $sql_result["col_number"] == 1,455.00
        number_format ($sql_result["col_number"], 2, ".", "");
        // could I get 1455 instead of 1455.00?
    ?>
    

    so my answer is if does exist some way to remove the decimals if I have DECIMAL data forma in my DB only when it's round?

    Or shoud I do something like that?

    <?php
        // $sql_result["col_number"] == 1,455.00
        str_replace(".00", "", (string)number_format ($sql_result["col_number"], 2, ".", ""));
        // will return 1455
    ?>
    
  • weaselskinghenry
    weaselskinghenry over 12 years
    I think there will be a bug using rtrim in this situation, for example, rtrim('1230.00','0.'); will give 123 rather than 1230.
  • Daniel Faria
    Daniel Faria almost 9 years
    casting to float is so far the best option
  • Michael Khalili
    Michael Khalili over 8 years
    Anyone who would want to apply this to a string should beware of values with commas like "1,000.99" Floating this value will return 1.
  • Farzher
    Farzher over 8 years
    You can simply add 0 1455.00 + 0 stackoverflow.com/questions/14531679/…
  • Cesc
    Cesc about 7 years
    The replace workaround will break with numbers expressed in european countries format, like Germany or Spain. For example 9,000 in Germany is 9.000,00 and this will become something like 90,00
  • Felix Eve
    Felix Eve over 5 years
    I'm surprised this doesn't have more up votes as is the cleanest / most elegant solution to this problem IMHO.
  • Hasenpriester
    Hasenpriester almost 5 years
    This is best way if you have other char for decimal separation.
  • anonymous007
    anonymous007 over 4 years
    this seem to have trimmed numbers like 10400.00 to 104 for me
  • Michael Khalili
    Michael Khalili over 4 years
    @YohanesAI I know. I'm warning people.
  • jso
    jso about 4 years
    The only problem is that money_format is deprecated by now and will be removed in future.. ref. php.net/money_format
  • Ken
    Ken over 3 years
    @bobo The bug does not exist with the code Halil gave here, that uses 2 rtrims executed in order.
  • Ray Coder
    Ray Coder over 3 years
    How to change decimals and thousands separator? @Farzher