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".

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀