货币格式
我对某些字符串格式有问题,我在格式化特定格式时强制文化:
get { return String.Format("{0:###,###,###,###,##0}", Convert.ToDecimal(_monthPay, new System.Globalization.CultureInfo("es-ES"))); }
这样我就可以得到这个:
$300.000,01
在本地主机上它工作正常,但是当我发布到服务器时,我得到这个:
$300,000.01
I不知道为什么!!!我无权访问服务器,因此无法更改服务器上的区域设置;还有其他方法可以解决吗?这样我就可以在本地主机上以及发布时正常工作?
谢谢。
i have a issue with some string formats, i'm forcing the culture when formatting specific formats:
get { return String.Format("{0:###,###,###,###,##0}", Convert.ToDecimal(_monthPay, new System.Globalization.CultureInfo("es-ES"))); }
so that i can get this:
$300.000,01
On localhost it works fine, but when i publish to the server, i get this:
$300,000.01
I don't know why!!! I don't have access to the server, so I can't change the regional settings on the server; is there another way to solve it? so that i works properly on localhost and when publishing?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将
CultureInfo
传递到了错误的位置。通过将
CultureInfo
传递给Convert.ToDecimal
,您可以告诉Convert.ToDecimal
使用该区域性转换数字。 (如果_monthPay
是字符串并且需要解析,则这是相关的)但是,您没有将
CultureInfo
传递给String.Format
,因此它仍然使用默认区域性。顺便说一句,如果您要组合多个值,则应该仅使用
String.Format
。在您的情况下,您应该调用ToString
重载。另外,您的格式字符串不必要地长;你可以简单地写#,0
。如果您想包含货币符号,只需使用C
即可。因此,您应该编写
Convert.ToDecimal(_monthPay).ToString("#,0", new System.Globalization.CultureInfo("es-ES"))
。You're passing the
CultureInfo
in the wrong place.By passing the
CultureInfo
toConvert.ToDecimal
, you're tellingConvert.ToDecimal
to convert the number using that culture. (This is relevant if_monthPay
is a string and needs to be parsed)However, you didn't pass a
CultureInfo
toString.Format
, so it is still using the default culture.By the way, you should only use
String.Format
if you're combining multiple values. In your case, you should call theToString
overload. Also, your format string is needlessly long; you can simply write#,0
. If you want to include a currency symbol, you can simply useC
instead.Therefore, you should write
Convert.ToDecimal(_monthPay).ToString("#,0", new System.Globalization.CultureInfo("es-ES"))
.您在这里所做的就是告诉 Convert.ToDecimal 函数 _monthPay 是什么样子。您所期望的是字符串将使用区域性信息进行格式化。
您应该告诉 String.Format 使用哪种区域性:
What you're doing here is telling the Convert.ToDecimal function what _monthPay will look like. What you're expecting is that the String will be formatted with the culture info.
You should be telling String.Format what culture to use:
尝试将其放入某个初始化块中。
Try putting that in some initialization block.
与其使用非常长的自定义格式,不如使用内置的货币格式呢?
编辑:移动文化信息..我仍然不认为使用一些大规模格式字符串是正确的。有内置的货币格式约定...
Instead of using the really long custom format, what about the built-in format for currency?
EDIT: moved the culture info.. I still don't think using some massive format string is right. There are built-in format conventions for currency...