Php number format decimal only if needed

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


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?


asked Nov 6, 2010 at 13:05

vittovitto

18.5k30 gold badges88 silver badges129 bronze badges

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

answered Oct 30, 2014 at 7:39

xDiffxDiff

6415 silver badges5 bronze badges

3

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.

answered Nov 6, 2010 at 13:08

Emil HEmil H

39.3k10 gold badges76 silver badges96 bronze badges

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[]:


answered May 6, 2011 at 21:11

Halil ÖzgürHalil Özgür

15.3k5 gold badges47 silver badges55 bronze badges

2

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]

answered Oct 23, 2016 at 5:05

Dan LeveilleDan Leveille

2,7712 gold badges23 silver badges28 bronze badges

2

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;

answered Sep 10, 2012 at 2:24

TomasTomas

2,9645 gold badges26 silver badges39 bronze badges

1

I've been accused of doing something like this:

 floatval[$foo] == intval[$foo] ? number_format[$foo] : number_format[$foo,2];

answered Feb 25, 2019 at 2:34

DanialDanial

1,34712 silver badges15 bronze badges

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

answered Oct 16, 2017 at 21:48

Kevin JKevin J

1741 silver badge11 bronze badges

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,36811 gold badges20 silver badges28 bronze badges

answered Sep 6, 2019 at 12:52

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;

answered Mar 18, 2014 at 21:29

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  0 && number_format[$value, $bestNumberOfDecimals] == number_format[$value, 0]] {
        $bestNumberOfDecimals = 0;
    }

    return number_format[$value, $bestNumberOfDecimals, $dec_point, $thousands_sep];
}

answered Dec 5, 2016 at 12:56

ThomasKThomasK

1956 silver badges11 bronze badges

What about

number_format[$value,2] - 0;

answered Mar 17, 2014 at 3:15

Warren.SWarren.S

1521 silver badge5 bronze badges

Not the answer you're looking for? Browse other questions tagged php string automation integer or ask your own question.

How do I limit decimal places in PHP?

echo[round[-4.40] . "
"];
echo[round[-4.60]];

How do you get only the decimal part of a number in PHP?

“how to get only decimal value in php” Code Answer.
$price = 1234.44;.
$whole = intval[$price]; // 1234..
$decimal1 = $price - $whole; // 0.44000000000005 uh oh! ... .
$decimal2 = round[$decimal1, 2]; // 0.44 this will round off the excess numbers..
$decimal = substr[$decimal2, 2]; // 44 this removed the first 2 characters..

How can I get only 2 decimal places in PHP?

Use number_format[] Function to Show a Number to Two Decimal Places in PHP..
Use round[] Function to Show a Number to Two Decimal Places in PHP..
Use sprintf[] Function to Show a Number to Two Decimal Places in PHP..
Related Article - PHP Number..

How remove extra zeros from decimal in PHP?

$num + 0 does the trick.

Chủ Đề