問題描述
我的代碼最初是為英語(yǔ)市場(chǎng)編寫的,其中小數(shù)點(diǎn)分隔符是."所以它期望數(shù)值作為字符串使用."作為分隔符.但是我們現(xiàn)在在其他地方也有用戶,例如歐洲的小數(shù)點(diǎn)分隔符是,"的地方.
I have code that was originally written for an English language market where the decimal separator is "." so it's expecting numeric values as strings to use "." as the separator. But we now have users in other places, e.g., places in Europe where the decimal separator is ",".
因此,在我的軟件(實(shí)際上只是當(dāng)前線程)的上下文中,我想將當(dāng)前語(yǔ)言的小數(shù)分隔符覆蓋為."即使它默認(rèn)為其他內(nèi)容.
So, in the context of my software (really just the current thread) I want to override the decimal separator for the current language to be "." even if it defaults to something else.
我試過了
String sep = ".";
NumberFormatInfo nfi1 = NumberFormatInfo.CurrentInfo;
nfi1.NumberDecimalSeparator = sep;
但我在第三行收到Instance is read-only"異常.顯然 NumberFormatInfo 是不可寫的.那么如何將當(dāng)前語(yǔ)言的小數(shù)點(diǎn)分隔符設(shè)置為默認(rèn)值以外的值呢?
But I get an "Instance is read-only" exception on the third line. Apparently NumberFormatInfo is not writable. So how DO you set the current language's decimal separator to something other than its default?
推薦答案
您需要?jiǎng)?chuàng)建一個(gè)新的文化,您可以使用當(dāng)前文化作為模板,只需更改分隔符.然后,您必須將當(dāng)前文化設(shè)置為新創(chuàng)建的文化,因?yàn)槟鸁o(wú)法直接更改當(dāng)前文化中的屬性.
You need to create a new culture and you can use the current culture as a template and only change the separator. Then you must set the current culture to your newly created one as you cannot change the property within current culture directly.
string CultureName = Thread.CurrentThread.CurrentCulture.Name;
CultureInfo ci = new CultureInfo(CultureName);
if (ci.NumberFormat.NumberDecimalSeparator != ".")
{
// Forcing use of decimal separator for numerical values
ci.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = ci;
}
這篇關(guān)于嘗試為當(dāng)前語(yǔ)言設(shè)置小數(shù)點(diǎn)分隔符,得到“Instance is read Only";的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!