显示浮点值不带科学记数法 [英] Display float value w/o scientific notation
问题描述
$ ret = 1.0 * 0.000000001;
我得到结果:1.0E-9
$ b
sprintf('%f',$ ret)
不起作用,它会返回 0.000000
。溢出?
sprintf('%f',$ ret)
不起作用,它会返回0.000000
。溢出?
sprintf
的作品,但是你错过了这里的一些观点。
0.000000
不会溢出。这只是 sprintf
对于%f
修饰符默认情况下使用6位数字。还请注意,%f
是可识别语言环境的,%F
可能更适合。
您可能需要使用更多数字,例如(400万):
$ php -rprintf('%。4000000F',1 * 0.000000001 );
注意:printf():在第1行的命令行代码中,4000000位数的请求精度被截断为PHP最大53位数
调用堆栈:
0.0001 319080 1. {main}()命令行代码:0
0.0001 319200 2. printf()命令行代码:1
$ b $ 0.00000000100000000000000006228159145777985641889706869
正如这个例子所示,不仅有一个共同的值(6位数),而且还有一个最大值(可能取决于PHP执行的计算机系统),这里因为你的问题,我会说你想要显示:
0.000000001
你需要这样写:
sprintf('%。9F',$ ret)
但是,您可能需要这样做:
rtrim(sprintf('%。20F',$ ret),'0');
这将从右边移除零:
0.000000001
希望这有帮助。 >
When i make the following multiplication in PHP:
$ret = 1.0 * 0.000000001;
i get the result: 1.0E-9
I want to convert this result into the normal decimal notation, how can i do this?
sprintf('%f',$ret)
doesn't work, it returns 0.000000
. Overflow?
sprintf('%f',$ret)
doesn't work, it returns0.000000
. Overflow?
sprintf
works, however you miss some point here.
0.000000
is not overflow. It's just that sprintf
for the %f
modifier uses 6 digits per default. Also please take care that %f
is locale aware, %F
is probably better suited.
You might want to use more digits, e.g. let's say 4 000 000 (four million):
$ php -r "printf('%.4000000F', 1*0.000000001);"
Notice: printf(): Requested precision of 4000000 digits was truncated to PHP maximum of 53 digits in Command line code on line 1
Call Stack:
0.0001 319080 1. {main}() Command line code:0
0.0001 319200 2. printf() Command line code:1
0.00000000100000000000000006228159145777985641889706869
As this example shows, there is not only a common value (6 digits) but also a maximum (probably depended on the computer system PHP executes on), here truncated to 53 digits in my case as the warning shows.
Because of your question I'd say you want to display:
0.000000001
Which are nine digits, so you need to write it that way:
sprintf('%.9F',$ret)
However, you might want to do this:
rtrim(sprintf('%.20F', $ret), '0');
which will remove zeroes from the right afterwards:
0.000000001
Hope this is helpful.
这篇关于显示浮点值不带科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!