Search code examples
phpfloating-pointprecisionfloating-accuracy

filter_var_array with floats causes too many decimal places in result


I have a problem with the filter_var_array function, in particular with the 'FILTER_VALIDATE_FLOAT' flag, which returns me floats with way too many decimal places

Code first:

if (property_exists($this->rawRequest, $varName)) {
    $res = filter_var_array($this->rawRequest->$varName, FILTER_VALIDATE_FLOAT);
}

This is my code, where I validate parameters that I received from a client via JSON
The rawRequest is a stdClass, like the following (printed with var_export):

stdClass::__set_state(
     array(
        'Action' => 'CreateObject',
        'Template' => 'none',
        'LatLng' => array (
            0 => '48.14914653539144',
            1 => '11.577623201171932',
        )
)

After the 'LatLng' parameter was filtered with filter_var_array, the result looks like the following:

'LatLng' => array (
    0 => 48.149146535391433732957206666469573974609375,
    1 => 11.5776232011719315551090403459966182708740234375,
)

Why does this happen?

I even can not fix this with round(), typecasting to float, to string and back to float, I tried to subtract 0.00000001 and then adding 0.00000001 again, nothing helps

UPDATE:
I wrote following little test script on the same machine, same apache server:

<?php

$test = '48.158308230848306';
$test2 = (float) $test;
$test3 = floatval($test);

echo($test2);
echo('<br />');
echo($test3);
exit(0);

output is here:

48.158308230848
48.158308230848

THAT is what I expect
I retested it with my prod code and used floatval() and typecasting to float, but it DID NOT work. I don't know, the only difference is that in the prod code the input comes as json and I parse it with the following code:

json_decode(file_get_contents('php://input'))

Solution

  • So, I finally found the answer to my problem

    The problem was never the filter_var_array() function, actually, the problem occurred far earlier.
    After retrieving the POST parameters via file_get_contents("php://input") everything is still fine.
    But when I decode the returned string with json_decode(), the float values are serialized in this process, resulting in the higher precision.

    I found two ways to "solve" this:

    • Change the "serialize_precision" parameter in the php.ini (mine was per default 100, setting it to the same as my "precision" parameter was successful)
      I DO NOT RECOMMEND this approach, since the effects of changing this parameter might be devastating somewhere else
    • Enclose the float in the request in quotes
      So change in your JSON {"test": 48.58495135846} to {"test": "48.58495135846"}
      This prevents the serialization of the float, as it is effectively a string now

    Hope this helps someone else, maybe this is already common knowledge, but I was tearing my hair out about this.