1

I need to use PHP floats in JSON string. Code:

$obj['val'] = '6.40';
json_encode($obj);

is converted to:

{"val": "6.40"}

It's OK - I have string value '6.40' in PHP and I have string value "6.40" in JSON.

The situation is not so good if I need to use floats:

$obj['val'] = 6.40;
json_encode($obj);

is converted to:

{"val": 6.4000000000000004}

but I need:

{"val": 6.40}

How can I convert PHP floats to JSON number in 'json_encode' with given precision?

Alex Gusev
  • 1,123
  • 2
  • 12
  • 28

0 Answers0