修改小数点和千位分隔符不改变小数位数 [英] Modify decimal point and thousands separator without changing the number of decimals
本文介绍了修改小数点和千位分隔符不改变小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 php 新手,我正在尝试使用 number_format :
I'm new to php and I'm trying to use number_format :
number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
如标题所示,我的目标是修改小数点和千位分隔符而不改变小数位数,如下所示:
As in the title, my goal is to modify decimal point and thousands separator without changing the number of decimals as below:
$Num=123456.789;
$Num2=number_format ($Num, [decimals as in $Num], ",", "'" );
我的结果应该是:
$Num2="123'456,789";
编辑
我需要一个未知小数位数的代码
I need a code for an unknown number of decimals
推荐答案
您可以使用 NumberFormatter.
您仍然需要指定一定数量的小数位数,但您可以使用足够高的值就可以了.这并不是说数字的数量真的是任意的.它与您的精度 ini 设置有关.
You will still need to specify a certain amount of fraction digits, but you can just use a high enough value and be fine. It's not like the number of digits is really arbitrary. It's tied to your precision ini setting.
$formatter = new NumberFormatter("en_US", NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 42);
$formatter->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, "'");
$formatter->setSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, ",");
echo $formatter->format(123456.7891234); // 123'456,7891234
这篇关于修改小数点和千位分隔符不改变小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文