How remove extra zeros from decimal in php?

Could anyone give me an explanation [and maybe an example] on how to strip the trailing zeros from a number using PHP.

For example:

"Lat":"37.422005000000000000000000000000","Lon":"-122.84095000000000000000000000000"

Would be turned in to:

"Lat":"37.422005","Lon":"-122.84095"

I am trying to strip the zeros to make it more readable. I tried using str_replace[] but this replaced the zeros inside the number too.

Arnaud

6,9098 gold badges49 silver badges69 bronze badges

asked Mar 1, 2011 at 0:13

2

Forget all the rtrims, and regular expressions, coordinates are floats and should be treated as floats, just prepend the variable with [float] to cast it from a string to a float:

$string = "37.422005000000000000000000000000";
echo [float]$string;

output:

37.422005

The actual result you have are floats but passed to you as strings due to the HTTP Protocol, it's good to turn them back into thier natural form to do calculations etc on.

Test case: //codepad.org/TVb2Xyy3

Note: Regarding the comment about floating point precision in PHP, see this: //stackoverflow.com/a/3726761/353790

answered Mar 1, 2011 at 0:23

RobertPittRobertPitt

56.2k21 gold badges113 silver badges159 bronze badges

9

Try with rtrim:

$number = rtrim[$number, "0"];

If you have a decimal number terminating in 0's [e.g. 123.000], you may also want to remove the decimal separator, which may be different depending on the used locale. To remove it reliably, you may do the following:

$number = rtrim[$number, "0"];
$locale_info = localeconv[];
$number = rtrim[$number, $locale_info['decimal_point']];

This of course is not a bullet-proof solution, and may not be the best one. If you have a number like 12300.00, it won't remove the trailing 0's from the integer part, but you can adapt it to your specific need.

answered Mar 1, 2011 at 0:18

jweyrichjweyrich

30k5 gold badges62 silver badges95 bronze badges

6

You'll run into a lot of trouble if you try trimming or other string manipulations. Try this way.

$string = "37.422005000000000000000000000000";

echo $string + 0;

Outputs:

37.422005

answered Jun 28, 2011 at 8:51

9

In my case, I did the following, extending other answers here:

rtrim[[strpos[$number,"."] !== false ? rtrim[$number, "0"] : $number],"."];

Because I also had to remove the decimal if a whole number was being shown

As an example, this will show the following numbers

2.00, 1.00, 28.50, 16.25 

As

2, 1, 28.5, 16.25

Rather than

2., 1., 28.5, 16.25

Which, to me, is not showing them correctly.

Latest edit also stops numbers such as "100" from being rtrim'ed to 1, by only trimming the rightmost 0's if a decimal is encountered.

answered Oct 17, 2012 at 23:24

Warren SergentWarren Sergent

2,4143 gold badges36 silver badges39 bronze badges

5

You can also use floatval[val];.

 

results in

37.422005

answered Mar 1, 2011 at 0:21

FarrayFarray

8,0543 gold badges32 silver badges37 bronze badges

2

Most of these solutions will trim significant digits in numbers such as "100" [no trailing decimal]. Here's a simple solution that doesn't have this problem:

function TrimTrailingZeroes[$nbr] {
    if[strpos[$nbr,'.']!==false] $nbr = rtrim[$nbr,'0'];
    return rtrim[$nbr,'.'] ?: '0';
}

I think that covers all the different scenarios.

>>> TrimTrailingZeroes['0']
=> "0"
>>> TrimTrailingZeroes['01']
=> "01"
>>> TrimTrailingZeroes['01.0']
=> "01"
>>> TrimTrailingZeroes['01.01']
=> "01.01"
>>> TrimTrailingZeroes['01.010']
=> "01.01"
>>> TrimTrailingZeroes['.0']
=> "0"
>>> TrimTrailingZeroes['.1']
=> ".1"
>>> TrimTrailingZeroes['.10']
=> ".1"
>>> TrimTrailingZeroes['3141592653589793.238462643383279502880000000000000000000000000000']
=> "3141592653589793.23846264338327950288"

answered Nov 4, 2013 at 21:43

mpenmpen

260k259 gold badges814 silver badges1183 bronze badges

2

If you want to strip the excess zeros away from the end but only to a certain decimal place, this is a useful expression to do just that [for example 0.30000 will show 0.30 and 0.0003000 will show 0.0003].

preg_replace["/[?

Chủ Đề