asp.net-mvc 使用 Razor 视图引擎 - 如何将十进制值格式化为包含逗号和两位小数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7087495/
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-09-08 01:24:56  来源:igfitidea点击:

Using Razor view engine - how do I format a decimal value to have commas and two decimal places?

asp.net-mvcstringformattingrazor

提问by BKahuna

As the title suggests - I have a value in my viewmodel that is decimal. I have no control over that. I'd like to display it as currency using the Razor View Engine.

正如标题所暗示的 - 我的视图模型中有一个十进制值。我对此无能为力。我想使用 Razor View Engine 将其显示为货币。

[email protected]("{0:0.00}", 1005.3422)

gets me part way there with:

让我在那里的一部分:

05.34

but how can I get the commas in there?

但是我怎么能得到逗号呢?

Thanks

谢谢

回答by Sumo

Can you use {0:c}instead? This is just standard string formatting in .NET and the "c" is for currency. There are lots of standard numeric string formats. And, of course, custom formatting, too.

你可以用{0:c}吗?这只是 .NET 中的标准字符串格式,“c”代表货币。有很多标准的数字字符串格式。当然,还有自定义格式

回答by Steve

$ @String.Format("{0:#,##0.00}", 1005.3422)

[email protected]("{0:#,##0.00}", 1005.3422)

回答by Sebastien F.

Most of the time, when you don't get the character you're expecting with strings conversion, it can be a locale issue. For exemple, you're developing with a en-us locale, but someone comes with a fr-FR locale. Then the date, currency, etc will be formatted and parsed differently.

大多数情况下,当您没有通过字符串转换获得您期望的字符时,这可能是语言环境问题。例如,您正在使用 en-us 语言环境进行开发,但有人使用 fr-FR 语言环境。然后日期、货币等将被不同地格式化和解析。