Weird float output in JSON

what’s wrong with my floats, any hints? :sob:

// templates/spaces.json.php
foreach($data as $space) {

  $json[] = [
    'spaceId' => (string)$space->id(),
    'uid' => (string)$space->uid(),
    'name' => (string)$space->title(),
    'district'  => (int)$space->district()->toInt(),
    'address'  => (string)$space->address(),
    'website'   => (string)$space->website(),
    'prettyurl' => (string)$space->website()->prettyUrl(),
    'inactive' => (bool)$space->inactive()->toBool(),
    'lat' => $space->lat()->tofloat(),
    'lng' => $space->lng()->tofloat(),



content file:

Lat: 48.194289


Lng: 16.36292


        "spaceId": "spaces/pina",
        "uid": "pina",
        "name": "Pina",
        "district": 4,
        "address": "Große Neugasse 44",
        "website": "",
        "prettyurl": "",
        "inactive": false,
        "lat": 48.194288999999997713530319742858409881591796875,
        "lng": 16.362919999999999021156327216885983943939208984375

That’s probably how the php.ini precision is set. Check with


This directive

kicks in when you (or PHP!) echo(), print(), cast as string or json_encode() a float variable

1 Like

Thanks! Will check this out

That’s not it, unfortunately. The settings are the same on my local server and on the live server, but it’s working locally.

// $ php -i | grep precision
precision => 14 => 14

Edit: also did echo ini_get('precision') on both to be sure but it’s the same result

Hm, you could simply round the result do get the number of decimals you want.

What about the `serialize_precision’ value?

$ php -i | grep precision
precision => 14 => 14
serialize_precision => -1 => -1

# just to double check...
$ php -r 'echo ini_get("serialize_precision");'

on both servers :confused:

yeah, I’ll probably just do that. Thanks anyways :~)

You could also try the JSON_PRESERVE_ZERO_FRACTION flag. What is your PHP version? Needless to say that I don’t have this issue… (with Kirby 3.5.3 and PHP 7.4.14)

good call. 7.4 locally, 7.3.27 on live and wondering why, actually – that’s not the latest stable version, is it?

# server
$ php -v
PHP 7.3.27

lol… shared hosting, always fun experiences!

probably my new settings are going to take a while to be applied but i hope this fixes it