79

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
?>
vitto
  • 19,094
  • 31
  • 91
  • 130

11 Answers11

49

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
xDiff
  • 691
  • 1
  • 5
  • 5
22

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.

Emil H
  • 39,840
  • 10
  • 78
  • 97
  • 12
    You can simply add 0 `1455.00 + 0` http://stackoverflow.com/questions/14531679/remove-useless-zero-digits-from-decimals-in-php – Farzher Jan 22 '16 at 23:43
  • 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 – Cesc Jun 20 '17 at 16:01
  • How to change decimals and thousands separator? @Farzher – Ray Coder Feb 24 '21 at 06:00
14

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
?>
Halil Özgür
  • 15,731
  • 6
  • 49
  • 56
  • 4
    I think there will be a bug using `rtrim` in this situation, for example, `rtrim('1230.00','0.');` will give 123 rather than 1230. – bobo Oct 21 '11 at 23:56
  • 1
    @bobo The bug does not exist with the code Halil gave here, that uses 2 rtrims executed in order. – Ken Oct 14 '20 at 08:29
9

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)
Dan Leveille
  • 3,001
  • 2
  • 24
  • 28
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;
Tomas
  • 3,054
  • 5
  • 27
  • 39
  • I'm surprised this doesn't have more up votes as is the cleanest / most elegant solution to this problem IMHO. – Felix Eve Nov 30 '18 at 08:58
2

I've been accused of doing something like this:

 floatval($foo) == intval($foo) ? number_format($foo) : number_format($foo,2);
Danial
  • 1,496
  • 14
  • 15
1

If you are targeting US currency I like to use this method:

function moneyform($number, $symbol = true) {
    return str_replace(".00", "", money_format(($symbol? '%.2n' : "%!n"), $number));
}

moneyform(1300999);
-->$1,300,999

moneyform(2500.99);
-->$2,500.99

moneyform(2500.99, false);
-->2,500.99
Kevin J
  • 194
  • 1
  • 11
  • 1
    The only problem is that `money_format` is deprecated by now and will be removed in future.. ref. https://www.php.net/money_format – jso May 27 '20 at 10:53
1

Mine since most quantity or pieces do not require decimal, this function will only show decimal when needed.

str_replace(".00", "", number_format($this->pieces, 2));
barbsan
  • 3,418
  • 11
  • 21
  • 28
Felix Labayen
  • 385
  • 3
  • 8
0

Warren.S answer helped me out. I didn't need the number_format function, so I just did this

$value=$value-0;

But in the OP's case, he needs number_format to remove the commas. So this would work for him

$value=number_format ($sql_result["col_number"], 2, ".", "")-0;
0

Since I could not find a flexible solution I wrote a simple function to get the best result:

function getValueFormattedWithMinimalDecimals($value, $max_decimals = 2, $dec_point = ',', $thousands_sep = '') {
    $bestNumberOfDecimals = -1;
    $decimal = 0;
    while ($decimal <= $max_decimals) {
        $bestNumberOfDecimals = $decimal;
        $valueDecimals = number_format($value, $decimal);
        if (floatval($value) == $valueDecimals) {
            break;
        }
        $decimal++;
    }
    if($bestNumberOfDecimals > 0 && number_format($value, $bestNumberOfDecimals) == number_format($value, 0)) {
        $bestNumberOfDecimals = 0;
    }

    return number_format($value, $bestNumberOfDecimals, $dec_point, $thousands_sep);
}
ThomasK
  • 195
  • 4
  • 11
-2

What about

number_format($value,2) - 0;
Warren.S
  • 152
  • 1
  • 5