doutan8506 2017-04-29 06:42
浏览 73

添加使用number_format()php函数形成的数字

When i try to add a integer to 3 digit number formed using php number_format() function give right result but when number become 4 digit it give wrong output.

I have tried this. please explain me the reason behind this??

$num1=number_format(1000.5,2);
$num1+=1;
echo $num1;

Output:2

But

  $num1=number_format(100.5,2);
  $num1+=1;
  echo $num1."
";`

Output:101.5

enter image description here

enter image description here

  • 写回答

3条回答 默认 最新

  • duanhan1392 2017-04-29 06:53
    关注

    number_format() returns a string not a number. It is there to format a numeric type (integer, float) to a string with a specific, desired "layout". So what you do is add the number 1 to the string resulting from number_format, which will try to cast the string back to a number, apparently resulting in 1 for the string cast as well, which gives you 2 total.

    tl;dr; Do calculations on numbers only and then do number_format at the very end to output in a defined format.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗