是否可以使用 Smarty PHP 将数字格式化为小数点后两位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:21:18  来源:igfitidea点击:

Is it possible to format a number to 2 decimal places with Smarty PHP

phpsmarty

提问by plosww1

Is it possible to format a number to 2 decimal places with Smarty PHP?

是否可以使用 Smarty PHP 将数字格式化为小数点后两位?

Thanks.

谢谢。

回答by jasonbar

string_formataccepts sprintf formatting options:

string_format接受 sprintf 格式选项:

{$number|string_format:"%.2f"}

回答by user889915

{$number|number_format:2}is what you want

{$number|number_format:2}是你想要的

回答by Wireblue

You can use the round function, though it will not add decimal places only remove them:

您可以使用 round 函数,但它不会添加小数位只会删除它们:

{$number|round:2}

{$number|round:2}

回答by mjhosawa

To display 2 instead of 2.00 you can use : replacein Smarty PHP

要显示 2 而不是 2.00,您可以使用:在 Smarty PHP 中替换

See below example -

见下面的例子 -

$number = 2.00

{$number|replace:'.00':''}

Output :- 2 

Hope this helps.

希望这可以帮助。