bruno
February 23, 2021, 10:48am
#1
what’s wrong with my floats, any hints?
// 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(),
];
}
echo json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
content file:
Lat: 48.194289
----
Lng: 16.36292
output:
{
"spaceId": "spaces/pina",
"uid": "pina",
"name": "Pina",
"district": 4,
"address": "Große Neugasse 44",
"website": "https://pinavienna.eu",
"prettyurl": "pinavienna.eu",
"inactive": false,
"lat": 48.194288999999997713530319742858409881591796875,
"lng": 16.362919999999999021156327216885983943939208984375
},
texnixe
February 23, 2021, 11:09am
#2
That’s probably how the php.ini precision is set. Check with
dump(ini_get('precision'));
This directive
kicks in when you (or PHP!) echo(), print(), cast as string or json_encode() a float variable
1 Like
bruno
February 23, 2021, 12:05pm
#3
Thanks! Will check this out
bruno
February 23, 2021, 6:44pm
#4
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
texnixe
February 23, 2021, 6:53pm
#5
Hm, you could simply round the result do get the number of decimals you want.
What about the `serialize_precision’ value?
bruno
February 23, 2021, 7:07pm
#6
$ php -i | grep precision
precision => 14 => 14
serialize_precision => -1 => -1
# just to double check...
$ php -r 'echo ini_get("serialize_precision");'
-1
on both servers
yeah, I’ll probably just do that. Thanks anyways :~)
texnixe
February 23, 2021, 7:20pm
#7
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)
bruno
February 23, 2021, 7:23pm
#8
good call. 7.4 locally, 7.3.27 on live and wondering why, actually – that’s not the latest stable version, is it?
texnixe
February 23, 2021, 7:26pm
#9
bruno
February 23, 2021, 7:26pm
#10
# one.com 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