dox90448 2012-12-18 10:13
浏览 82
已采纳

Sprintf浮动一个带有右边填充零的18,16浮点数

I'm trying to get float (18,16) with padding zeros to the right, but this isn't working because it always ends with a 1 instead of a zero. My guess it's because I've specified it wrong but I'm not sure how to do it correct.

Sample: 12.12234 must become 12.122340000000000 and 1.01 must become 1.01000000000000.

To achieve this I'm using sprintf('%18.016f', $fMyFloat); but this always ends with a 1 instead of a 0. Obviously I'm overseeing something, I just don't know what. Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douren6874 2012-12-18 10:33
    关注

    This test code:

    $data = array(
        12.12234,
        1.01
    );
    foreach($data as $fMyFloat){
        echo sprintf('%18.016f', $fMyFloat) . PHP_EOL;
    }
    

    ... prints this in my 64-bit computer:

    12.1223399999999994
    1.0100000000000000
    

    You are facing the well-known problem of floating point precision. Converting integers from base 10 to base 2 is pretty straightforward (and exact) but base 10 floating point numbers cannot always be represented exactly in base 2. This issue isn't specific to PHP but there's a warning in the PHP manual:

    Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

    Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision

    Your only chance to achieve such precision is to manipulate numbers as strings. PHP bundles two arbitrary precision libraries that can help you when you need to do match with strings: GNU Multiple Precision and BC Math. Here's a little hack with bcmath:

    $data = array(
        '12.12234',
        '1.01'
    );
    bcscale(16);
    foreach($data as $fMyFloat){
        echo bcdiv($fMyFloat, 1) . PHP_EOL;
    }
    

    It this case, though, you can probably use simple string functions:

    $data = array(
        '12.12234',
        '1.01'
    );
    foreach($data as $fMyFloat){
        list($a, $b) = explode('.', $fMyFloat);
        echo $a . '.' . str_pad($b, 16, 0) . PHP_EOL;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀