Trying to set the decimal separator for the current language, getting “Instance is read Only”

后端 未结 2 1472
温柔的废话
温柔的废话 2020-12-19 02:32

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

相关标签:
2条回答
  • 2020-12-19 03:11

    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;
     }
    
    0 讨论(0)
  • 2020-12-19 03:12

    You can use the Clone() method on the NumberFormatInfo instance, which will create a mutable version (i.e. IsReadOnly = false). You are then able set the currency symbol and/or other number format options:

    string sep = "."; 
    NumberFormatInfo nfi1 = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
    nfi1.NumberDecimalSeparator = sep;
    
    0 讨论(0)
提交回复
热议问题