字符串格式货币 [英] String format currency

查看:153
本文介绍了字符串格式货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的行

@String.Format("{0:C}", @price)

在我的剃刀视图。我希望它显示在价格前面的美元符号,而是它显示一个井号。我如何做到这一点?

in my razor view. I want it to display a dollar sign in front of the price but instead it display a pound sign. How do I achieve this?

推荐答案

我强烈怀疑问题很简单,就是线程处理请求的当前文化没有设置正确。

I strongly suspect the problem is simply that the current culture of the thread handling the request isn't set appropriately.

您可以将它设置为整个请求,或指定的文化,而格式化。无论哪种方式,我建议的没有的使用的String.Format 用复合材料的格式,除非你真的有一个以上的东西格式化(或更广泛的信息)。相反,我会使用:

You can either set it for the whole request, or specify the culture while formatting. Either way, I would suggest not use string.Format with a composite format unless you really have more than one thing to format (or a wider message). Instead, I'd use:

@price.ToString("C", culture)

这只是使它更简单。

It just makes it somewhat simpler.

编辑:鉴于您的评论,这听起来像你可能想使用的用户的文化,英国文化的无论的。如此反复,要么设置英国文化作为整个请求的线程的文化,或可能与推出了自己的助手一个常量:

Given your comment, it sounds like you may well want to use a UK culture regardless of the culture of the user. So again, either set the UK culture as the thread culture for the whole request, or possibly introduce your own helper class with a "constant":

public static class Cultures
{
    public static readonly CultureInfo UnitedKingdom = 
        CultureInfo.GetCultureInfo("en-GB");
}

然后:

@price.ToString("C", Cultures.UnitedKingdom)

在我的经验中,有一个命名集文化,这样使得code。使用它相当简单阅读,你并不需要得到正确的字符串在多个地方。

In my experience, having a "named" set of cultures like this makes the code using it considerably simpler to read, and you don't need to get the string right in multiple places.

这篇关于字符串格式货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆