<bdo id='XKYkz'></bdo><ul id='XKYkz'></ul>

    1. <tfoot id='XKYkz'></tfoot>

      <small id='XKYkz'></small><noframes id='XKYkz'>

    2. <i id='XKYkz'><tr id='XKYkz'><dt id='XKYkz'><q id='XKYkz'><span id='XKYkz'><b id='XKYkz'><form id='XKYkz'><ins id='XKYkz'></ins><ul id='XKYkz'></ul><sub id='XKYkz'></sub></form><legend id='XKYkz'></legend><bdo id='XKYkz'><pre id='XKYkz'><center id='XKYkz'></center></pre></bdo></b><th id='XKYkz'></th></span></q></dt></tr></i><div id='XKYkz'><tfoot id='XKYkz'></tfoot><dl id='XKYkz'><fieldset id='XKYkz'></fieldset></dl></div>
      1. <legend id='XKYkz'><style id='XKYkz'><dir id='XKYkz'><q id='XKYkz'></q></dir></style></legend>

        php浮点数计算小数点后2位

        php float calculation 2 decimal point(php浮点数计算小数点后2位)
          <tbody id='MGiJd'></tbody>

        <small id='MGiJd'></small><noframes id='MGiJd'>

            <legend id='MGiJd'><style id='MGiJd'><dir id='MGiJd'><q id='MGiJd'></q></dir></style></legend>
            <i id='MGiJd'><tr id='MGiJd'><dt id='MGiJd'><q id='MGiJd'><span id='MGiJd'><b id='MGiJd'><form id='MGiJd'><ins id='MGiJd'></ins><ul id='MGiJd'></ul><sub id='MGiJd'></sub></form><legend id='MGiJd'></legend><bdo id='MGiJd'><pre id='MGiJd'><center id='MGiJd'></center></pre></bdo></b><th id='MGiJd'></th></span></q></dt></tr></i><div id='MGiJd'><tfoot id='MGiJd'></tfoot><dl id='MGiJd'><fieldset id='MGiJd'></fieldset></dl></div>
              1. <tfoot id='MGiJd'></tfoot>
                  <bdo id='MGiJd'></bdo><ul id='MGiJd'></ul>

                  本文介绍了php浮点数计算小数点后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有一道数学计算题.

                  $a = 34.56
                  
                  $b = 34.55
                  

                  $a做一些计算得到这个数字

                  $a do some calculation to get this figure

                  $b 正在四舍五入到最接近的 0.05 以获得这个数字

                  $b is doing rounding to the nearest 0.05 to get this figure

                  发生了什么

                  $c = $b - $a
                  

                  应该是-0.01,但是我回显了$c,它显示了-0.00988888888888

                  supposedly it be -0.01, but I echo out the $c, which shows -0.00988888888888

                  我尝试使用number_format($c, 2),但是输出是0.00,

                  I try to use number_format($c, 2), but the output is 0.00,

                  如何确保 $a$b 正好是 2 位小数,后面没有隐藏数字.

                  how can I make sure $a and $b is exactly 2 decimals, no hidden number at the back.

                  在我的php知识中,number_format只能格式化显示,但值并不是真正的2位小数,

                  in my php knowledge, number_format is only able to format the display, but the value is not really 2 decimal,

                  我希望我能从这里得到帮助.这让我很沮丧.

                  I hope I can get help from here. This really frustrated me.

                  推荐答案

                  试试 sprintf("%.2f", $c);

                  浮点数以 IEEE 表示法表示,基于 2 的幂,因此终止十进制数可能不是终止二进制数,这就是您得到尾随数字的原因.

                  Floating point numbers are represented in IEEE notation based on the powers of 2, so terminating decimal numbers may not be a terminating binary number, that's why you get the trailing digits.

                  正如可变长度编码器所建议的那样,如果您知道所需的精度并且它不会改变(例如,当您处理金钱时),最好只使用定点数,即将数字表示为美分而不是比美元

                  As suggested by Variable Length Coder, if you know the precision you want and it doesn't change (e.g. when you're dealing with money) it might be better to just use fixed point numbers i.e. express the numbers as cents rather than dollars

                  $a = 3456;
                  
                  $b = 3455;
                  
                  $c = $b - $a;
                  
                  sprintf ("%.2f", $c/100.0);
                  

                  这样,如果您在打印前进行大量计算,就不会出现任何舍入错误.

                  This way, you won't have any rounding errors if you do a lot of calculations before printing.

                  这篇关于php浮点数计算小数点后2位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Laravel 4 - Connect to other database(Laravel 4 - 连接到其他数据库)
                  Call external API function from controller, LARAVEL 4(从控制器调用外部 API 函数,LARAVEL 4)
                  Empty string instead of null values Eloquent(空字符串而不是空值 Eloquent)
                  quot;laravel.logquot; could not be opened: failed to open stream(“laravel.log无法打开:无法打开流)
                  Displaying the Error Messages in Laravel after being Redirected from controller(从控制器重定向后在 Laravel 中显示错误消息)
                  Laravel Creating Dynamic Routes to controllers from Mysql database(Laravel 从 Mysql 数据库创建到控制器的动态路由)

                    <tbody id='ystlm'></tbody>
                  <tfoot id='ystlm'></tfoot>

                        <bdo id='ystlm'></bdo><ul id='ystlm'></ul>
                      • <i id='ystlm'><tr id='ystlm'><dt id='ystlm'><q id='ystlm'><span id='ystlm'><b id='ystlm'><form id='ystlm'><ins id='ystlm'></ins><ul id='ystlm'></ul><sub id='ystlm'></sub></form><legend id='ystlm'></legend><bdo id='ystlm'><pre id='ystlm'><center id='ystlm'></center></pre></bdo></b><th id='ystlm'></th></span></q></dt></tr></i><div id='ystlm'><tfoot id='ystlm'></tfoot><dl id='ystlm'><fieldset id='ystlm'></fieldset></dl></div>

                          1. <legend id='ystlm'><style id='ystlm'><dir id='ystlm'><q id='ystlm'></q></dir></style></legend>

                            <small id='ystlm'></small><noframes id='ystlm'>