<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位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    <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'>