这是我今天学到的一些陷阱 - 我的代码验证我的发现如下。我将系统设置为西班牙语 - 多米尼加共和国,并将货币符号从“RD$”更改为“$”。然后我将我的文化/UI 文化设置为 es-DO 而不是 en-US。
使用number.ToString("C2")
不遵循用户自定义设置。因此,如果您将System.Globalization.CultureInfo.CurrentCulture
/设置CurrentUICulture
为 es-DO 和 do 1.00M.ToString("C2")
,它将返回 RD$1.00。
要让它实际使用用户自定义设置,您必须有一个var info = new CultureInfo("es-DO")
变量,然后string.Format(info, "{0:C2}", 1.00M)
根据上面的更改使用来获得预期的 1.00 美元。
您不能使用string.Format(CultureInfo.CurrentCulture, "{0:C2}", 1.00M)
,因为它不会返回用户自定义 - 在这种情况下,它将返回 RD$1.00。
我认为这很愚蠢,但如果有人知道这种行为背后的真正原因,请分享,因为我很好奇。
这是复制的代码:
var esdo = new CultureInfo("en-US");
{en-US}
esdo = new CultureInfo("es-DO");
{es-DO}
var resdo = new RegionInfo("es-DO");
{es-DO}
CurrencyEnglishName: "Dominican Peso"
CurrencyNativeName: "Peso"
CurrencySymbol: "$"
DisplayName: "Dominican Republic"
EnglishName: "Dominican Republic"
GeoId: 65
IsMetric: true
ISOCurrencySymbol: "DOP"
Name: "es-DO"
NativeName: "República Dominicana"
ThreeLetterISORegionName: "DOM"
ThreeLetterWindowsRegionName: "DOM"
TwoLetterISORegionName: "DO"
esdo.NumberFormat
{System.Globalization.NumberFormatInfo}
CurrencyDecimalDigits: 2
CurrencyDecimalSeparator: "."
CurrencyGroupSeparator: ","
CurrencyGroupSizes: {int[1]}
CurrencyNegativePattern: 1
CurrencyPositivePattern: 0
CurrencySymbol: "$"
DigitSubstitution: None
IsReadOnly: false
NaNSymbol: "NeuN"
NativeDigits: {string[10]}
NegativeInfinitySymbol: "-Infinito"
NegativeSign: "-"
NumberDecimalDigits: 2
NumberDecimalSeparator: "."
NumberGroupSeparator: ","
NumberGroupSizes: {int[1]}
NumberNegativePattern: 1
PercentDecimalDigits: 2
PercentDecimalSeparator: "."
PercentGroupSeparator: ","
PercentGroupSizes: {int[1]}
PercentNegativePattern: 1
PercentPositivePattern: 1
PercentSymbol: "%"
PerMilleSymbol: "‰"
PositiveInfinitySymbol: "Infinito"
PositiveSign: "+"
var dsa = 1.00M;
string.Format(esdo,"{0:C2}",dsa)
"$1.00"
dsa.ToString("C2")
"RD$1.00"
string.Format(System.Globalization.CultureInfo.CurrentCulture,"{0:C2}",dsa)
"RD$1.00"
string.Format(System.Globalization.CultureInfo.CurrentUICulture,"{0:C2}",dsa)
"RD$1.00"
System.Globalization.CultureInfo.CurrentUICulture
{es-DO}
string.Format(esdo,"{0:C2}",dsa)
"$1.00"