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.
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:output:
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: http://codepad.org/TVb2Xyy3
Note: Regarding the comment about floating point precision in PHP, see this: https://.com/a/3726761/353790