1

Possible Duplicate:
How to convert culture specific double using TypeConverter?

I am getting an exception while trying to parse an "1.000.000" string to Double by using TypeConverter. I looked at the System.Globalization.NumberFormatInfo at the moment of exception and it looks like this:

{System.Globalization.NumberFormatInfo}
    CurrencyDecimalDigits: 2
    CurrencyDecimalSeparator: ","
    CurrencyGroupSeparator: "."
    CurrencyGroupSizes: {int[1]}
    CurrencyNegativePattern: 8
    CurrencyPositivePattern: 3
    CurrencySymbol: "TL"
    DigitSubstitution: None
    IsReadOnly: false
    NaNSymbol: "NaN"
    NativeDigits: {string[10]}
    NegativeInfinitySymbol: "-Infinity"
    NegativeSign: "-"
    NumberDecimalDigits: 2
    NumberDecimalSeparator: ","
    NumberGroupSeparator: "."
    NumberGroupSizes: {int[1]}
    NumberNegativePattern: 1
    PercentDecimalDigits: 2
    PercentDecimalSeparator: ","
    PercentGroupSeparator: "."
    PercentGroupSizes: {int[1]}
    PercentNegativePattern: 2
    PercentPositivePattern: 2
    PercentSymbol: "%"
    PerMilleSymbol: "‰"
    PositiveInfinitySymbol: "Infinity"
    PositiveSign: "+"

Everyting seems fine to parse "1.000.000" but it says "1.000.000" is not a valid value for Double. What is the problem? I tried to override Thread.CurrentThread.CurrentCulture but it did not work either.

EDITED :::::::::

This seems to solve my problem as well. TypeConverter actually works without ThousandSeperator. I added one and it started to work.

possible duplicate of How to convert culture specific double using TypeConverter? – Rasmus Faber How to convert culture specific double using TypeConverter?

4

2 回答 2

2

试试这个NumberFormatInfo

var s = "1.000.000";
var info = new NumberFormatInfo
{
    NumberDecimalSeparator = ",", 
    NumberGroupSeparator = "."
};
var d = Convert.ToDouble(s, info);

您可以更改NumberDecimalSeparator为其他内容,只要它不同于NumberGroupSeparator.

编辑:NumberFormatInfo您指定的应该也可以工作。

于 2013-01-04T19:37:20.173 回答
1

Most normal numerical types have parse methods. Use TryParse if you're unsure if it's valid (Trying to parse "xyz" as a number will throw an exception)

For custom parsing you can define a NumberFormatInfo like this:

var strInput = "1.000.000";
var numberFormatInfo = new NumberFormatInfo
{
    NumberDecimalSeparator = ",",
    NumberGroupSeparator = "."
};
double dbl = Double.Parse(strInput, numberFormatInfo);

这个解决方案也可以

var format = new System.Globalization.NumberFormatInfo();
format.NumberDecimalSeparator = ",";
format.NumberGroupSeparator = ".";
double dbl2 = Double.Parse("1.000.000", format);
于 2013-01-04T19:51:03.007 回答