小数货币格式 [英] Decimal money format

查看:139
本文介绍了小数货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将所有数字转换为xx,xx十进制格式而又不爆炸数字或字符串? 喜欢;

how to convert all numbers to xx,xx decimal format without exploding number or string? like;

8,9 --> 8,90

8,99 --> 8,99

12,1 --> 12,10

129,9 --> 129,90

有什么主意吗?

推荐答案

您可以使用数字格式,例如:

You can use number_format like:

$n = 2.1;
echo number_format($n, 2, ','); // 2,10

如果您在输入中使用逗号作为小数点分隔符,则可以将值转换为浮点值:

If you have commas as decimal separators in your input you can convert values to float with:

$number = floatval(str_replace(',', '.', str_replace('.', '', $string_number)));

str_replace('.', '', $string_number)用于删除数千个分隔符. str_replace(',', '.', ... )用于用点替换逗号.

str_replace('.', '', $string_number) is used to remove thousand separators. str_replace(',', '.', ... ) is used to replace commas with dots.

这篇关于小数货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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