1

重複の可能性:
TypeConverter を使用してカルチャ固有の double を変換する方法は?

Doubleを使用して「1.000.000」文字列を解析しようとすると、例外が発生しTypeConverterます。System.Globalization.NumberFormatInfo例外の瞬間を見たところ、次のようになりました。

{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 は "1.000.000" を解析するのに問題ないように見えますが、"1.000.000" は有効な値ではないと表示されDoubleます。何が問題ですか?上書きしようとしましThread.CurrentThread.CurrentCultureたが、うまくいきませんでした。

編集済み :::::::::

これは私の問題も解決するようです。TypeConverter は実際には ThousandSeperator がなくても機能します。1つ追加したところ、機能し始めました。

TypeConverter を使用してカルチャ固有の double を変換する方法の重複の可能性はありますか? – Rasmus Faber TypeConverter を使用してカルチャ固有の double を変換する方法は?

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 に答える