显示不带科学计数法的浮点值 [英] Display float value w/o scientific notation

查看:24
本文介绍了显示不带科学计数法的浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 PHP 中进行以下乘法运算时:

When i make the following multiplication in PHP:

$ret = 1.0 * 0.000000001;

我得到结果:1.0E-9

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) 不起作用,它返回 0.000000.溢出?

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

推荐答案

sprintf('%f',$ret) 不起作用,它返回 0.000000.溢出?

sprintf('%f',$ret) doesn't work, it returns 0.000000. Overflow?

sprintf 有效,但是您在这里错过了一些要点.

sprintf works, however you miss some point here.

0.000000 没有溢出.只是 %f 修饰符的 sprintf 默认使用 6 位数字.另外请注意 %f 可以识别区域设置,%F 可能更适合.

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.

您可能想要使用更多数字,例如假设是 4 000 000(四百万):

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

如本例所示,不仅有一个公共值(6 位),还有一个最大值(可能取决于 PHP 执行的计算机系统),在我的例子中,如警告所示,这里截断为 53 位.

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');

之后会从右边删除零:

0.000000001

希望这有帮助.

这篇关于显示不带科学计数法的浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆