C# asp.net 中的货币格式字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/759425/
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
Currency format string in asp.net
提问by Mr W
I am using an infragistics webgrid and need to format a currency string. For this I need a string containing a pattern such as "$ ### ###,00" and I would like this to come out of my current CultureInfo. How can I do this? Do I need to compose it manually from the info in:
我正在使用基础设施网络网格,需要格式化货币字符串。为此,我需要一个包含诸如“$ ### ###,00”之类的模式的字符串,并且我希望它从我当前的 CultureInfo 中出来。我怎样才能做到这一点?我是否需要从以下信息手动编写它:
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyGroupSeparator
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyGroupSizes
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyDecimalDigits
CultureInfo.CreateSpecificCulture(myLanguageId).NumberFormat.CurrencyDecimalSeparator
etc etc etc
等等等等
Is the a one-line solution?
是单线解决方案吗?
采纳答案by Mr W
thanks for all your answers. It turns out that my requirements were wrong. The infragistics grid does not want a pattern string to know which separators use, it uses the pattern string for decimals and such and then queries the current culture about the rest. So it is a non-issue. thanks anyway!
感谢您的所有回答。事实证明我的要求是错误的。infragistics 网格不希望模式字符串知道使用哪些分隔符,它使用模式字符串表示小数等,然后查询有关其余部分的当前文化。所以这不是问题。不管怎么说,还是要谢谢你!
回答by Greg Dean
decimal moneyvalue = 1921.39m;
string s = String.Format("{0:C}", moneyvalue);
The current culture will be used.
将使用当前文化。
Make sure you have the following in your web.config:
确保您的 web.config 中有以下内容:
<system.web>
<globalization culture="auto" uiCulture="auto"/>
</system.web>
or as ck suggests, declare the equivalent, in your page
或者按照 ck 的建议,在您的页面中声明等效项
回答by cjk
Adding to Greg Dean's answer, you may need to have
添加到 Greg Dean 的回答中,您可能需要
Culture="auto" UICulture="auto"
in the @Page declaration of your page.
在页面的@Page 声明中。
回答by Marc Gravell
First - if you are being culture specific, why not just use "C" as the format string, and hence use the culture's currency format.
首先 - 如果您是特定于文化的,为什么不使用“C”作为格式字符串,从而使用文化的货币格式。
Custom numeric format stringsdon't have a currency token; I suspect, however, that you could just append the currency symbolto the string you get from formatting with "### ###,00".
自定义数字格式字符串没有货币标记;但是,我怀疑您可以将货币符号附加到使用“### ###,00”进行格式化的字符串中。
回答by Mr W
We had almost similar requirements in our proj. what we did was to use webcurrencyedit control of infragistics, set it as the editorcontrolid for the column for which currency had to be shown and then set the culture of webcurrencyedit control.
我们在我们的项目中有几乎相似的要求。我们所做的是使用基础设施的 webcurrencyedit 控件,将其设置为必须显示货币的列的 editorcontrolid,然后设置 webcurrencyedit 控件的文化。