使用Razor视图引擎 - 如何格式化十进制值以使用逗号和两个小数位?

时间:2022-06-10 08:08:23

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.

正如标题所示 - 我的viewmodel中有一个十进制值。我无法控制。我想使用Razor View Engine将其显示为货币。

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

gets me part way there with:

让我顺其自然:

$1005.34

but how can I get the commas in there?

但我怎么能在那里得到逗号?

Thanks

谢谢

3 个解决方案

#1


26  

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”代表货币。有许多标准的数字字符串格式。当然,也是自定义格式。

#2


2  

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语言环境。然后,日期,货币等将被格式化和解析不同。

#3


1  

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

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

#1


26  

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”代表货币。有许多标准的数字字符串格式。当然,也是自定义格式。

#2


2  

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语言环境。然后,日期,货币等将被格式化和解析不同。

#3


1  

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

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