加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

.sprintf(“%4.2f”,value)如何在PHP中运行?

发布时间:2020-12-13 21:46:49 所属栏目:PHP教程 来源:网络整理
导读:我正在学习更多的 PHP教程,特别是 DevZone PHP 101,我很困惑: echo .sprintf("%4.2f",(2 * $radius * pi())) 我找到了这个 I think that means produce a floating-point field four positions wide with two decimal places,using the value of the first
我正在学习更多的 PHP教程,特别是 DevZone PHP 101,我很困惑:

echo .sprintf("%4.2f",(2 * $radius * pi()))

我找到了这个

I think that means produce a floating-point field four positions wide with two decimal places,using the value of the first succeeding parameter.

That comes from the C/C++ line of programming languages. an sprintf() takes the first parameter as a format statement. Anything in it starting with a % is a field specifier; anything else is just printable text. So if you give a format statement with all text and no specifiers,it will print exactly the way it appears. With format specifiers,it needs data to work on.

但在尝试了一些不同的价值后,我仍然没有得到它.在我看来,如果在这种情况下它的目的只是将小数限制为2个位置,我必须放置

.sprintf("%.2f",(2 * $radius * pi()))

前面的4点有什么意义?在PHP Manual中,它让我相信它确定字符的总数应该是4但是(a)不是这种情况,因为小数点使它成为5个字符,而(b)不是这种情况,因为我尝试将其更改为更大像%8.2f这样的数字并没有在两端都添加任何零.有人可以更好地解释一下.

谢谢!

解决方法

格式说明符中的第一个数字%8.2f用于填充长度.默认情况下,sprintf使用空格字符.

你可以看到更大数字的效果:

printf("%20.2f",1.23);

例如会导致:

1.23

这个号码前面有16个空格.浮动占用4,填充长度设置为20. (也许你把它打印到网页上,因此看不到填充空间..)

sprintf联机帮助页面下面还有一个示例,可以使用替代填充字符:

printf("%'*20.2f",1.23); // use the custom padding character '*'

将导致:

****************1.23

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读