<tfoot id='DNLnM'></tfoot>

      <bdo id='DNLnM'></bdo><ul id='DNLnM'></ul>
  • <legend id='DNLnM'><style id='DNLnM'><dir id='DNLnM'><q id='DNLnM'></q></dir></style></legend>

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

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

      1. 浮点到 int 类型转换的 PHP 意外结果

        PHP unexpected result of float to int type cast(浮点到 int 类型转换的 PHP 意外结果)
        <tfoot id='pRffd'></tfoot>

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

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

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

                  本文介绍了浮点到 int 类型转换的 PHP 意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图在 php 中将浮点数转换为 int 值:

                  var_dump((int)(39.3 * 100.0));//返回3929但应该是3930!var_dump((int)(39.2 * 100.0));//返回3920

                  我可以使用 ceil 使其工作,但有人可以向我解释一下吗?

                  var_dump((int)ceil(39.3 * 100.0));//返回3930

                  解决方案

                  这是因为以 10 为底的有限表示的数字在 PHP 使用的浮点表示中可能有也可能没有精确表示.

                  <前>>php -r "echo var_dump(sprintf('%.40F', 39.3 * 100.0));"字符串(45) "3929.9999999999995452526491135358810424804688"

                  由于 int 总是将数字向下舍入,因此表示中的一个小错误会使转换将其向下舍入一个您所期望的数字.

                  考虑使用 round 代替.

                  I'trying to convert a float to an int value in php:

                  var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930!
                  var_dump((int)(39.2 * 100.0)); //Returns 3920
                  

                  I can use ceil to make it work but can somebody explain this to me?

                  var_dump((int)ceil(39.3 * 100.0)); //Returns 3930
                  

                  解决方案

                  This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses.

                  See

                  >php -r "echo var_dump(sprintf('%.40F', 39.3 * 100.0));"
                  string(45) "3929.9999999999995452526491135358810424804688"
                  

                  Since int always rounds the number down, a small error in the representation makes the cast round it one number down that you would otherwise expect.

                  Consider using round instead.

                  这篇关于浮点到 int 类型转换的 PHP 意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

                1. <small id='jQ7cH'></small><noframes id='jQ7cH'>

                      <bdo id='jQ7cH'></bdo><ul id='jQ7cH'></ul>
                          <tbody id='jQ7cH'></tbody>
                        <legend id='jQ7cH'><style id='jQ7cH'><dir id='jQ7cH'><q id='jQ7cH'></q></dir></style></legend>

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