C# 货币格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1071273/
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 formatting
提问by Ryan
This should be an easy problem but...
这应该是一个简单的问题,但是......
I need to format a currency for display (string) in C#
我需要在 C# 中格式化显示(字符串)的货币
The currency in question will have its own rules such as the symbol to use and if that symbol should come before the value (e.g. $ 10) or after (e.g. 10 ? which is Vietnamese Dong).
有问题的货币将有自己的规则,例如要使用的符号,以及该符号是应该在值之前(例如 $10)还是之后(例如 10 ?这是越南盾)。
But how the numbers are formatted depends upon the users local, not the currency.
但是数字的格式取决于本地用户,而不是货币。
E.g.
例如
1.234,56 ? should be displayed to a user in Vietnam but
1,234.56 ? should be displayed to a user in the US
(formatted as code so easy to see difference between , and.)
(格式化为代码,很容易看出 , 和 之间的区别。)
So code like
所以代码就像
Double vietnamTotal = 1234.56;
return vietnamTotal.ToString("c");
Won't work as it will use the users (or more accuratly CultureInfo.CurrentCulture) locale for format and currency so you would get things like $1,123.56 - right use of , and . but wrong symbol.
不会起作用,因为它将使用用户(或更准确地说是 CultureInfo.CurrentCulture)语言环境来设置格式和货币,因此您会得到 1,123.56 美元之类的东西 - 正确使用 , 和 。但错误的符号。
Double vietnamTotal = 1234.56;
CultureInfo ci = new CultureInfo(1066); // Vietnam
return vietnameTotal.ToString("c",ci));
Would give 1.234,56 ? - Right symbol, wrong use of , and . for current user.
会给 1.234,56 吗?- 正确的符号,错误地使用 , 和 。对于当前用户。
This post gives more detailon the right thing to do, but not how to do it.
这篇文章详细介绍了正确的做法,但没有详细说明如何去做。
What obvious method hidden in the framework am I missing?
我错过了什么隐藏在框架中的明显方法?
采纳答案by Jon Skeet
- Take the
NumberFormatInfo
from the user's currency, and clone it - Set the
CurrencySymbol
in the cloned format to theCurrencySymbol
of the currency in question - If you want the currency position (and some other aspects of the format) to be copied,
set
CurrencyPositivePattern
andCurrencyNegativePattern
in the same way. - Use the result to format.
NumberFormatInfo
从用户的货币中取出,并克隆它- 将
CurrencySymbol
克隆格式中的 设置CurrencySymbol
为相关货币的 - 如果您希望复制货币头寸(以及格式的其他一些方面),请以相同的方式设置
CurrencyPositivePattern
和CurrencyNegativePattern
。 - 使用结果进行格式化。
For example:
例如:
using System;
using System.Globalization;
class Test
{
static void Main()
{
decimal total = 1234.56m;
CultureInfo vietnam = new CultureInfo(1066);
CultureInfo usa = new CultureInfo("en-US");
NumberFormatInfo nfi = usa.NumberFormat;
nfi = (NumberFormatInfo) nfi.Clone();
NumberFormatInfo vnfi = vietnam.NumberFormat;
nfi.CurrencySymbol = vnfi.CurrencySymbol;
nfi.CurrencyNegativePattern = vnfi.CurrencyNegativePattern;
nfi.CurrencyPositivePattern = vnfi.CurrencyPositivePattern;
Console.WriteLine(total.ToString("c", nfi));
}
}
Admittedly my console doesn't manage to display the right symbol, but I'm sure that's just due to font issues :)
诚然,我的控制台无法显示正确的符号,但我确定这只是由于字体问题:)
回答by Black Light
Sorry, I may be being a little slow here, but I still don't see your point. It seems to me that the original question is "I have a dong value which, in Vietnam, I want displayed as per Vietnamese currency format, with a "?" symbol, and in the US, I want displayed as per US currency format, but still with a "?".".
抱歉,我在这里可能有点慢,但我仍然没有明白你的意思。在我看来,最初的问题是“我有一个 dong 值,在越南,我希望按照越南货币格式显示,并带有“?”符号,而在美国,我希望按照美国货币格式显示,但仍然带有“?”。.
I guess I'm getting confused by the two contradictory statements... "The currency in question will have its own rules"and "how the numbers are formatted depends upon the users local [sic], not the currency."
我想我对这两个相互矛盾的陈述感到困惑...... “有问题的货币将有自己的规则”和“数字的格式取决于本地用户[原文如此],而不是货币。”
If all you wanted to change was the currency symbol, and leave the formatting as per the current culture, wouldn't cloning the nfi and setting the symbol be enough ?
如果您只想更改货币符号,并根据当前文化保留格式,那么克隆 nfi 并设置符号还不够吗?
NumberFormatInfo nfi;
// pretend we're in Vietnam
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("vi-VN");
nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;
nfi.CurrencySymbol = "?";
String s1 = (1234.5678).ToString("c", nfi);
// pretend we're in America
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
nfi = CultureInfo.CurrentCulture.NumberFormat.Clone() as NumberFormatInfo;
nfi.CurrencySymbol = "?";
String s2 = (1234.5678).ToString("c", nfi);
回答by Kozack Lixeer
To work with a textbox, try the following:
要使用文本框,请尝试以下操作:
textBox2.Text = "Rp" + (textBox1.Text = string.Format(System.Globalization.CultureInfo.GetCultureInfo("id-ID"), "{0:#,##0.00}", double.Parse(textBox1.Text)));