json_decode is rounding floats, how can I prevent it? json_decode is rounding floats, how can I prevent it? php php

json_decode is rounding floats, how can I prevent it?


I had the same problem. I solved it using the followin regex

SOLUTION 1

$yourJsonVariable = preg_replace('/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', ': "$1"', $yourJsonVariable);

Convert it into array

$array = json_decode($yourJsonVariable, true);

Credits goes to this SO LINK

SOLUTION 2

You can set ini_set('precision',1);

SOLUTION 3

$decoded = json_decode($encoded, true, null, JSON_BIGINT_AS_STRING);

NOTE: The Last solution will work only for PHP > 5.4

You might want to take a look at this Blog


Just wrap the values in quotes: json_decode('[["3.2","1"],["4.8","2"]]');