1

I have a an object like

{"latitude":-37.81425094604492, "longitude":144.96316528320312}

and I obtain

$latitude = $myobj->latitude;

but the value that is returned is -37.814250946045 , with 12 digits fractional part (rounded to 12 digits from the original 14)

how can I obtain the actual -37.81425094604492 with all 14 digits and no rounding?

haz
  • 740
  • 1
  • 11
  • 20
  • 2
    The only way I've seen is to convert them to strings in the JSON (https://stackoverflow.com/a/8759547/1213708 although may need some adjustments to the regex). Not ideal, but at least it gives the values. – Nigel Ren May 27 '20 at 14:37
  • @NigelRen is right, the only way to get the "full" number without conversion to float and losing precision is, to edit the json, so php parses them as strings. – Frederick Behrends May 27 '20 at 14:49
  • yes gives values but does not account for negatives - if anyone wants to post a regex that works ill accept as answer – haz May 27 '20 at 15:23
  • 1
    Does this answer your question? [PHP - float numbers wrong precision with json\_decode](https://stackoverflow.com/questions/47509864/php-float-numbers-wrong-precision-with-json-decode) – bestprogrammerintheworld May 27 '20 at 18:36
  • yes that did the trick - $latitude = number_format($myobj->latitude,14); – haz May 28 '20 at 00:35

1 Answers1

2

It seems that you need to keep the serialization precision to 17, and this is a reasonable and feasible precision.

In php.ini , there are configuration argument precision looks like this:

; The number of significant digits displayed in floating point numbers.
precision = 14

Change it to what you want, like:

precision = 17

And the json_decode should keep the precision you need.

Hope this answer helps.

tgarm
  • 473
  • 3
  • 8
  • thanks tgarm - good answer but prefer not to have to edit the php.ini file - the number_format solution works best – haz May 28 '20 at 00:47