duandi1636 2016-07-29 22:07
浏览 90
已采纳

number_format()无法转换科学记数法

I have some float values being looped through an array that are outputting in scientific notation.

I need to convert the scientific notation to a regular flat float long notation value.

I tried number_format(), but it still outputs in scientific notation until the very last cycle of my loop?

<?php
$parray = array(array('BTC-EXP','1',0.000097,1),array('BTC-VOX',7,number_format(0.000075,8),7));
for ($x = 0; $x <= 1; $x++) {
        var_dump($parray);
        foreach($parray as $pairp){
                echo "
Debug info! Limit
";
                echo $pairp[2];
                echo "
";
                print_r(number_format($pairp[2],8));

        }
}

?>

output:

Debug info! midpoint
array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(7) "BTC-EXP"
    [1]=>
    string(1) "1"
    [2]=>
    float(9.7E-5)
    [3]=>
    int(1)
  }
  [1]=>
  array(4) {
    [0]=>
    string(7) "BTC-VOX"
    [1]=>
    int(7)
    [2]=>
    string(10) "0.00007500"
    [3]=>
    int(7)
  }
}

Debug info! Limit
9.7E-5
0.00009700
Debug info! Limit
0.00007500
0.00007500
Debug info! midpoint
array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(7) "BTC-EXP"
    [1]=>
    string(1) "1"
    [2]=>
    float(9.7E-5)
    [3]=>
    int(1)
  }
  [1]=>
  array(4) {
    [0]=>
    string(7) "BTC-VOX"
    [1]=>
    int(7)
    [2]=>
    string(10) "0.00007500"
    [3]=>
    int(7)
  }
}

Debug info! Limit
9.7E-5
0.00009700
Debug info! Limit
0.00007500
0.00007500

How do I convert my float values to long notation through an entire cycle (deny short notation)?

  • 写回答

1条回答 默认 最新

  • drg17404 2016-07-30 03:32
    关注

    In your $parray, you are storing one number as a float (0.000097) and in the other you're storing the result of number_format(0.000075,8), which is a string.

    It's not surprising then, that when you call echo $pairp[2]; on 0.000097, that you see 9.7E-5, but when you echo it for number_format(0.000075,8) you see a string representation (0.00007500).

    Let's do the same for print_r(number_format($pairp[2],8));. When it's called for float (0.000097), it formats it as a string and you see 0.00009700. When it's called for "0.00007500", this string is converted to a float 0.000075, which is then converted back to a string "0.00007500".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大