PHP number: decimal point visible only if needed
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;
![vitto](https://i.stack.imgur.com/wVINJ.png?s=256&g=1)
Comments
-
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 over 12 yearsI 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 almost 9 yearscasting to float is so far the best option
-
Michael Khalili over 8 yearsAnyone 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 over 8 yearsYou can simply add 0
1455.00 + 0
stackoverflow.com/questions/14531679/… -
Cesc about 7 yearsThe 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 over 5 yearsI'm surprised this doesn't have more up votes as is the cleanest / most elegant solution to this problem IMHO.
-
Hasenpriester almost 5 yearsThis is best way if you have other char for decimal separation.
-
anonymous007 over 4 yearsthis seem to have trimmed numbers like 10400.00 to 104 for me
-
Michael Khalili over 4 years@YohanesAI I know. I'm warning people.
-
jso about 4 yearsThe only problem is that
money_format
is deprecated by now and will be removed in future.. ref. php.net/money_format -
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 over 3 yearsHow to change decimals and thousands separator? @Farzher