PHP,如果数字是整数,则添加尾随零 [英] php, add trailing zeros if the number is integer

查看:133
本文介绍了PHP,如果数字是整数,则添加尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单数学:

  $a=$b/$c; echo $a;

如果$ b等于123.00,$ c等于1,则$ a变为123.

if $b equals to 123.00 and $c equals to 1 then $a becomes 123.

如果$ b为123.50,则$ c为1,$ a为123.50.但在前一种情况下,我希望$ a为123.00.

If $b is 123.50, $c is 1, $a is 123.50. But in the former case , I want $a to be 123.00.

可以测试$ a是否具有任何非零小数部分,然后根据需要添加尾随零.

It is possible to test whether $a has any non-zero fraction part or not, and then add the trailing zeros as necessary.

但是我正在寻找php函数来做同样的事情.有可能吗?

But I am looking for php functions to do the same thing. Possible?

如果我不希望number_format出现逗号怎么办?

What if I do not want the commas from number_format there ?

推荐答案

使用sprintf("%0.2f",$a);. 文档

这篇关于PHP,如果数字是整数,则添加尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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