Edgar
Edgar

Reputation: 187

Json_decode limit

I'm using json_decode() but for some reason it trims the last digits, example:

$test = '[{"Endereco_POI":"","UF":"SP","IBGE_N":"3509502","IBGE":"350950","Municipio":"CAMPINAS","Bairro":"","CEP":"1305883","Numero":"","X":-47.17788692505713,"Y":-22.918751685387484,"Prioridade":6,"Score":100.0,"Erro":347.59,"EnderecoEncontrado":"1305883, CAMPINAS SP"}]';

echo "<pre>";
print_r(json_decode($test, TRUE));
echo "</pre>";

the output is

Array
(
    [0] => Array
        (
            [Endereco_POI] => 
            [UF] => SP
            [IBGE_N] => 3509502
            [IBGE] => 350950
            [Municipio] => CAMPINAS
            [Bairro] => 
            [CEP] => 1305883
            [Numero] => 
            [X] => -47.177886925057
            [Y] => -22.918751685387
            [Prioridade] => 6
            [Score] => 100
            [Erro] => 347.59
            [EnderecoEncontrado] => 1305883, CAMPINAS SP
        )

)

so insted of -47.17788692505713 it outputs -47.177886925057.

Whats the reason for that and how do I fix it?

Upvotes: 1

Views: 175

Answers (2)

Dave
Dave

Reputation: 5190

Checking the php.ini file setting for:

precision

By default it is set to 14. From the manual Description of core php.ini directives:

The number of significant digits displayed in floating point numbers.

Upvotes: 2

Marc B
Marc B

Reputation: 360872

You have more decimal places than PHP supports:

php > $y = -22.918751685387484;
php > echo $y;
-22.918751685387

If you need those trailing digits, then the float values have to encoded as strings inside the JSON:

php > $y = "-22.918751685387484";
php > echo $y;
-22.918751685387484

Upvotes: 1

Related Questions