How to convert culture specific double using TypeConverter?

前端 未结 1 561
难免孤独
难免孤独 2020-12-19 23:16

I have a problem with the TypeConverter class. It works fine with CultureInvariant values but cannot convert specific cultures like English thousan

相关标签:
1条回答
  • 2020-12-19 23:47

    The DoubleConverter that is obtained from TypeDescriptor.GetConverter(typeof(double)) end ups calling Double.Parse with the following arguments:

    Double.Parse(
        "2,999.95", 
        NumberStyles.Float, 
        (IFormatProvider)culture.GetFormat(typeof(NumberFormatInfo)));
    

    The problem is that NumberStyles.Float does not allows thousands separators and that's why you are getting the problem. You can submit this on Microsoft Connect or see if anybody else had the same problem.

    If Double.Parse is called also with NumberStyles.AllowThousands the problem would not occur.

    Double.Parse(
        "2,999.95", 
        NumberStyles.Float | NumberStyles.AllowThousands, 
        (IFormatProvider)culture.GetFormat(typeof(NumberFormatInfo)));
    
    0 讨论(0)
提交回复
热议问题