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

      <legend id='iSjOC'><style id='iSjOC'><dir id='iSjOC'><q id='iSjOC'></q></dir></style></legend>

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

        <tfoot id='iSjOC'></tfoot>

      1. 用 PHP 截断浮点数

        Truncate float numbers with PHP(用 PHP 截断浮点数)

        <tfoot id='xj3af'></tfoot>
            <bdo id='xj3af'></bdo><ul id='xj3af'></ul>

                <legend id='xj3af'><style id='xj3af'><dir id='xj3af'><q id='xj3af'></q></dir></style></legend>
                1. <small id='xj3af'></small><noframes id='xj3af'>

                    <tbody id='xj3af'></tbody>
                  <i id='xj3af'><tr id='xj3af'><dt id='xj3af'><q id='xj3af'><span id='xj3af'><b id='xj3af'><form id='xj3af'><ins id='xj3af'></ins><ul id='xj3af'></ul><sub id='xj3af'></sub></form><legend id='xj3af'></legend><bdo id='xj3af'><pre id='xj3af'><center id='xj3af'></center></pre></bdo></b><th id='xj3af'></th></span></q></dt></tr></i><div id='xj3af'><tfoot id='xj3af'></tfoot><dl id='xj3af'><fieldset id='xj3af'></fieldset></dl></div>
                  本文介绍了用 PHP 截断浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当一个浮点数需要在浮点数后截断到某位时,事实证明这并不容易.例如,如果必须截断到该点之后的第二个数字,则数字应该是

                  When a float number needs to be truncated to a certain digit after the floating point, it turns out that it is not easy to be done. For example if the truncating has to be done to second digit after the point, the numbers should be

                  45.8976 => 45.89, 0.0185 => 0.01
                  

                  (点后第二位不按点后第三位四舍五入).

                  ( second digit after the point is not rounded according to the third digit after the point ).

                  round()number_format()sprintf()等函数将数字取整并打印出来

                  Functions like round(), number_format(), sprintf() round the number and print out

                  45.8976 => 45.90, 0.0185 => 0.02
                  

                  我遇到了两种解决方案,我想知道它们是否足够好,哪个更好用

                  I have met two solutions and I am wondering if they are good enough and which one is better to be used

                  1.

                  function truncNumber( $number, $prec = 2 )
                  {
                      return bccomp( $number, 0, 10 ) == 0 ? $number : round( $number - pow( 0.1, bcadd(   $prec, 1 ) ) * 5, $prec );
                  }
                  

                  2.

                  function truncNumber($number, $prec = 2 )
                  {
                      return sprintf( "%.".$prec."f", floor( $number*pow( 10, $prec ) )/pow( 10, $prec ) );
                  }
                  

                  推荐答案

                  floor 会按照你的要求去做.

                  floor will do as you ask.

                  floor(45.8976 * 100) / 100;
                  

                  你不会找到更直接的函数,因为这是一种奇怪的问题.通常你会在数学上四舍五入.出于好奇 - 你需要这个做什么?

                  You won't find a more direct function for this, since it's a kind of odd thing to ask. Normally you'll round mathematically correct. Out of curiosity - What do you need this for?

                  这篇关于用 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 的问题)
                  <tfoot id='bxQhW'></tfoot>
                    <bdo id='bxQhW'></bdo><ul id='bxQhW'></ul>
                    1. <i id='bxQhW'><tr id='bxQhW'><dt id='bxQhW'><q id='bxQhW'><span id='bxQhW'><b id='bxQhW'><form id='bxQhW'><ins id='bxQhW'></ins><ul id='bxQhW'></ul><sub id='bxQhW'></sub></form><legend id='bxQhW'></legend><bdo id='bxQhW'><pre id='bxQhW'><center id='bxQhW'></center></pre></bdo></b><th id='bxQhW'></th></span></q></dt></tr></i><div id='bxQhW'><tfoot id='bxQhW'></tfoot><dl id='bxQhW'><fieldset id='bxQhW'></fieldset></dl></div>

                        <tbody id='bxQhW'></tbody>

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

                        <legend id='bxQhW'><style id='bxQhW'><dir id='bxQhW'><q id='bxQhW'></q></dir></style></legend>