doushanmo7024 2017-03-17 07:20
浏览 78
已采纳

带浮点数的filter_var_array会导致结果中出现过多的小数位

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'))
  • 写回答

1条回答 默认 最新

  • duanfei1930 2017-03-30 09:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看