使用 php money 格式并删除 GBP 小数点后的数字 [英] Use php money format and remove the numbers after the decimal place for GBP
问题描述
我想使用 GB 货币格式将我的分析中的一些数字格式化为货币,但作为仪表板的格式,不需要那么精确,所以我想删除便士(小数点后的数字)并四舍五入它有助于 css 布局,我该怎么做?向上或向下舍入到最接近的磅数就可以了.
I want to format some numbers from my analytics into currency using the GB money format but as its for a dashboard and doesn't need to be that precise so I want to remove the pence (numbers after the decimal place) and round it which helps with the css layout, how do I do this? Rounding up or down to the nearest pound would be fine.
我的代码如下:
//set locale for currency
setlocale(LC_MONETARY, 'en_GB');
$sales = '8932.83';
echo utf8_encode(money_format('%n', $sales));
输出:8,932.83 英镑
This outputs: £8,932.83
但是,我如何将其四舍五入以输出为 8,932 英镑,小数点后没有任何内容.
However how do I round this to be output as just £8,932 without anything after the decimal place.
我想使用货币格式,因为有时数字是负数,在这种情况下,money_format 返回的数字如 -£8,932.83 比 -8,932(英镑和负号以错误的方式)更可取,这是我格式化时发生的情况像这样使用 number_format:
I want to use currency format as sometimes the figure is negative in which case money_format returns the number like -£8,932.83 which is preferable to £-8,932 (pound and negative symbol around the wrong way) which is what happened when I formatted using number_format like so:
echo '£'.number_format($sales, 0, '', ',');
推荐答案
你是要四舍五入还是去掉小数点?
Do you want to round it or get rid of the decimals?
将其四舍五入为 8933 将是:
To round it which would be 8933 would be:
echo utf8_encode(money_format('%.0n', $sales));
要去掉小数,你可以使用 floor(四舍五入):
To get rid of the decimals, you could use floor (which rounds down):
echo utf8_encode(money_format('%.0n', floor($sales)));
这篇关于使用 php money 格式并删除 GBP 小数点后的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!