使用 Razor 视图引擎 - 如何格式化小数值以包含逗号和两位小数?
正如标题所示 - 我的视图模型中有一个十进制值。我对此无法控制。我想使用 Razor View Engine 将其显示为货币。
[email protected]("{0:0.00}", 1005.3422)
让我分道扬镳:
$1005.34
但是我怎样才能把逗号放在那里呢?
谢谢
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.
[email protected]("{0:0.00}", 1005.3422)
gets me part way there with:
$1005.34
but how can I get the commas in there?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
{0:c}
代替吗?这只是 .NET 中的标准字符串格式,“c”代表货币。有很多标准数字字符串格式。当然,还有自定义格式。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.大多数时候,当您在字符串转换中没有获得所需的字符时,可能是区域设置问题。例如,您正在使用 en-us 语言环境进行开发,但有人使用 fr-FR 语言环境。然后日期、货币等将以不同的方式进行格式化和解析。
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.
理想情况下,您将从某个模型中获取视图数据,例如
public Decimal ItemPrice { get;放; 在这种
情况下,视图中的 Razor 表达式可以是
@Model.ItemPrice.ToString("c")
同样可以用于简单的 ViewBag 项目,例如
@ViewBag .ItemPrice.ToString("c")
Ideally you'd be fetching the view data from some model e.g.
public decimal ItemPrice { get; set; }
In which case the Razor expression in your view could be
@Model.ItemPrice.ToString("c")
The same can be used for simple ViewBag items, e.g.
@ViewBag.ItemPrice.ToString("c")
或者您可以在类定义中添加
Or you can add at class definition