PHP中的简单正则表达式,格式为十进制数字 [英] simple regex in php, formatting decimal number

查看:55
本文介绍了PHP中的简单正则表达式,格式为十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数字转换为后跟.00,但前提是该数字是整数,或者小数点后只有1个数字,如下所示:

I need to convert numbers to have .00 after them, but only if the number is an integer, or it has just 1 number after the decimal point, like so:

1.4 = 1.40
45 = 45.00
34.77 = 34.77

在这种简单情况下要使用什么reg exp?

What reg exp to use for this simple case?

推荐答案

number_format($number, 2, '.', '');

PHP.net 中了解更多信息.您无需确定数字是否是整数-只要是数字,它就会被格式化为两位小数.

Read more at PHP.net. You don't need to determine if a number is an integer or not -- as long as it's a number, it will be formatted to two decimal places.

如果要使用千位分隔符,请将最后一个参数更改为','.

If you'd like the thousands separator, change the last parameter to ','.

这篇关于PHP中的简单正则表达式,格式为十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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