1

PHP seems to have a bug in the way it handles decimal precision in json_encode.

It's easy to see just by encoding a simple float:

echo json_encode(["testVal" => 54932.00]);

// Prints out:
{"testVal": 54932}

How I can get result like this:

{"testVal": 54932.00}

I need two decimals places (including .00) in json.

Jadasdas
  • 869
  • 4
  • 19
  • 39
  • 2
    But this is the same value. What do you want to accomplish? If you want to get the exact formatting, then store the value as a string. If you want the numeric value - then `54932` and `54932.00` is the same. – Adrian Kokot Oct 29 '21 at 07:28
  • 1
    Does this answer your question? [json\_encode retain float values as is it is](https://stackoverflow.com/questions/41298424/json-encode-retain-float-values-as-is-it-is) – Justinas Oct 29 '21 at 07:29
  • @AdrianKokot Actually if using strict comparison, then it's not the same. One is float and another is int, so when using `private float $testVal` it would throw error – Justinas Oct 29 '21 at 07:30
  • @AdrianKokot I need send this on service, I need in json be 54932.00 – Jadasdas Oct 29 '21 at 07:32

0 Answers0