显示不带科学计数法的浮点值

2023-10-31php开发问题
2

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

问题描述

当我在 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

希望这有帮助.

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17