How to remove trailing zeros in PHP

Hi, can someone give me an explanation (and maybe an example) of how I can go about removing numbered zeros from a number using PHP.

For example:

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

Will be translated into:

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

I am trying to remove zeros to make it more readable. I tried using str_replace() , but it replaced the zeros inside the number (doh).

Thanks = D

+59
string php
Mar 01 2018-11-11T00:
source share
16 answers

Forget all rtrims and regular expressions, the coordinates are float and should be treated as a float, just add the variable with (float) to drop it from the string into the float:

 $string = "37.422005000000000000000000000000"; echo (float)$string; 

exit:

 37.422005 

The actual result that you have is floats, but passed to you as strings due to the HTTP protocol, it is good to return them to their natural form for calculations, etc.

Test case: http://codepad.org/TVb2Xyy3

Note As for the comment on floating point precision in PHP, see this: https://stackoverflow.com/a/166778/

+110
Mar 01 2018-11-11T00:
source share

Try using rtrim :

 $number = rtrim($number, "0"); 

If you have a decimal number ending in 0 (for example, 123.000), you can also remove the decimal separator , which may differ depending on the locale used . To securely remove it, you can do the following:

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

This, of course, is not a bulletproof solution, and perhaps not the best. If you have a number, for example 12300.00, it will not remove the trailing 0 from the integer part, but you can adapt it to your specific need.

+25
Mar 01 2018-11-11T00
source share

You will have problems if you try trimming or other string manipulations. Try this way.

 $string = "37.422005000000000000000000000000"; echo $string + 0; 

Outputs:

 37.422005 
+20
Jun 28. 2018-11-11T00:
source share

You can also use floatval(val); .

  <?php echo floatval( "37.422005000000000000000000000000" ); ?> 

leads to

37.422005

+7
Mar 01 2018-11-11T00:
source share

I know this question was asked in 2012, but I just found this simple solution ...

 $result = $num + 0; 

So for this question it will be:

 $lat = 37.422005000000000000000000000000 + 0; //37.422005 $lon = -122.84095000000000000000000000000 + 0; //-122.84095 
+5
Feb 13 '14 at 20:22
source share

If you want to remove extra zeros from the end, but only to a certain decimal place, this is a useful expression to do this (for example, 0.30000 will show 0.30 and 0.0003000 will show 0.0003).

 preg_replace("/(?<=\\.[0-9]{2})[0]+\$/","",$number); 

Of course, {2} controls the decimal point limit.

+4
Jun 22 2018-12-18T00:
source share

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

 rtrim((strpos($number,".") !== false ? rtrim($number, "0") : $number),"."); 

Because I also had to remove the decimal if an integer was shown

As an example, this will show the following numbers

 2.00, 1.00, 28.50, 16.25 

how

 2, 1, 28.5, 16.25 

Instead

 2., 1., 28.5, 16.25 

Which, for me, does not show them correctly.

Last editing also stops numbers, such as "100", from rtrim'ed to 1, only trimming the rightmost 0 if a decimal is encountered.

+4
Oct 17 '12 at 23:24
source share

Most of these solutions will truncate significant digits in numbers, such as "100" (without a finite decimal number). Here is a simple solution that does not have this problem:

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

I think this 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" 
+4
Nov 04 '13 at
source share

Truncates trailing zeros, but only after the decimal point - for example, we would not want to convert 100 β†’ 1. It will also divide the period if only zeros follow it.

 function trim_zeros($str) { if(!is_string($str)) return $str; return preg_replace(array('`\.0+$`','`(\.\d+?)0+$`'),array('','$1'),$str); } 
+3
Aug 19 2018-12-12T00:
source share

You should use a circular function that is more capable of manipulating numbers than replace .

+1
Mar 01 2018-11-11T00:
source share

For me, I need another solution for converting exponential values ​​and primes such as 10000 to the last non-zero significant digit

 /* Convert any value to the least significant value */ function toDecimal($val) { // Convert any exponential sign to proper decimals $val = sprintf("%lf", $val); if (strpos($val, '.') !== false) { $val = rtrim(rtrim($val, '0'), '.'); } return $val; } // Test cases echo toDecimal(1000.000) . "\n"; echo toDecimal(1E-5) . "\n"; echo toDecimal(1E+5) . "\n"; echo toDecimal(1234.56) . "\n"; echo toDecimal(1234.5700) . "\n"; echo toDecimal(1234000) . "\n"; echo toDecimal(-1e10) . "\n"; // Results 1000 0.00001 100000 1234.56 1234.57 1234000 -10000000000 
+1
Dec 04 '12 at 11:21
source share

Like Okonomiyaki3000, instead of adding, you can also multiply by 1, for example

 $string = "37.422005000000000000000000000000"; echo $string*1;die; // outputs - 37.422005 
+1
Jun 01 '14 at 2:43
source share

You can use rtrim () ( http://www.php.net/manual/en/function.rtrim.php ):

 rtrim( "37.422005000000000000000000000000" , "0" ) 

Just make sure there will be a decimal point.

0
Mar 01 2018-11-11T00:
source share

Hmm, interesting. I used this:

 // remove leading zeros $output = ltrim ($output, "0"); // remove trailing 0s. $temp=explode(".",$output); $temp[1]=rtrim($temp[1],"0"); $output = $temp[0]; if (!empty($temp[1])) $output.='.'.$temp[1]; return $output; 

This removes trailing 0s from the number after the first decimal point. Otherwise, the number 100 is 1.

Since I did the comparison, using the float method caused problems. Hope this helps?

0
Sep 19 '12 at 13:43 on
source share

If you want a solution that accepts the minimum and maximum number of decimal places, this should work:

 /** * Displays up to 4 decimal places, with a minimum of 2 decimal places, trimming unnecessary zeroes * * @param mixed $number * @param int $minimumDecimals * @param int $maximumDecimals * @return string */ function rate_format($number, $minimumDecimals = 2, $maximumDecimals = 4): string { $formatted = number_format($number, $maximumDecimals, '.', ','); $minimumLength = strpos($formatted, '.') + $minimumDecimals + 1; $extra = rtrim(substr($formatted, $minimumLength), "0"); return substr($formatted, 0, $minimumLength) . $extra; } // rate_format("1.2500") returns "1.25" // rate_format("1.2525") returns "1.2525" // rate_format("1.25256") returns "1.2526" // rate_format("1") returns "1.00" 

It is hard-coded for use in the United States, but should be easily adapted for other languages.

0
Feb 05 '19 at 15:25
source share

preg_replace will do the trick:

 $lat=preg_replace('/0+$/','',$lat); 
-2
Mar 01 2018-11-11T00:
source share



All Articles