0

How can I get for example:

{"latitude": 35.123000,"longitude": 41.123500,"height": 100.00}

Instead of:

{"latitude": 35.123,"longitude": 41.1235,"height": 100}

I get this output with the following code:

ini_set('serialize_precision', 6);
...
// calculate latitude, longitude and height
...
$response->payload->latitude = rad2deg($latitude);
$response->payload->longitude = rad2deg($longitude);
$response->payload->height = $height;
// some other fields in response..

print(json_encode($response, JSON_PRETTY_PRINT));

I also tried using (float)number_format(...) but could not get what I want.

no more sigsegv
  • 468
  • 5
  • 17
  • I personally do understand how to make a more precise number less precise, but vice-versa? – nicael Apr 07 '22 at 14:06
  • The downvote is here because it's unclear what you're asking about. `serialize_precision` is about significant digits (not zeros). – nicael Apr 07 '22 at 14:16
  • If all you want is keep extra zeros then the question is absolutely basic. – nicael Apr 07 '22 at 14:17
  • I know, but I have to come up with what I've tried right? I wouldn't be asking if I knew what to use instead. – no more sigsegv Apr 07 '22 at 14:19
  • Duplicate of: https://stackoverflow.com/questions/20670114/what-is-the-exact-equivalent-of-js-something-tofixed-in-php – nicael Apr 07 '22 at 14:21
  • 1
    That's not a duplicate, nobody has to know JavaScript's .toFixed to find a php solution. – no more sigsegv Apr 07 '22 at 14:37
  • your post is duplicate not because of how the linked question is formulated, but because it has an answer to your question – nicael Apr 07 '22 at 14:43
  • Formatting a float to have less precision is just that, _formatting_. And formatting is for _human eyes_, not data serialization and transfer. Abandon the notion of making JSON pretty to look at, and format the data for display only when you are _about to display it_. – Sammitch Apr 07 '22 at 18:10

1 Answers1

-1

You should run your values through number_format.

$response->payload->latitude = number_format(rad2deg($latitude), 8);
$response->payload->longitude = number_format(rad2deg($longitude), 8);
$response->payload->height = number_format($height, 2);

Output:

{
    "latitude": "35.12266800",
    "longitude": "41.12348100",
    "height": "100.00"
}

Keep in mind that this will convert your floats into strings.

Besworks
  • 4,123
  • 1
  • 18
  • 34
  • I downvoted because it's clearly a [duplicate question](https://stackoverflow.com/questions/20670114/what-is-the-exact-equivalent-of-js-something-tofixed-in-php) and such questions should be closed, not answered – nicael Apr 07 '22 at 14:22
  • Hi, I'm not the downvoter. I avoid outputting numeric values as string. This is why in the question, I said I tried casting `number_format` to float – no more sigsegv Apr 07 '22 at 14:29
  • @nomore numbers with trailing excess zeros don't exist, they're not numbers. It's the only solution to the problem. – nicael Apr 07 '22 at 14:45
  • 1
    As far as the PHP interpreter is concerned. `35.123 === 35.12300000`. The only difference is how they are displayed, which you can control with `number_format`. Storing the value with trailing zeros only uses up extra bytes on the filesystem. Instead of writing the formatted values into your json file you should format the stored floats at output time. – Besworks Apr 07 '22 at 14:49