2013-01-04 2 views
1

가능한 중복 :
How to convert culture specific double using TypeConverter?1.000.000는 더블 C#에 대한 유효한 값이 아닙니다

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없이 작동합니다. 나는 하나를 추가했고 그것은 일하기 시작했다.

가능한 복제 TypeConverter를 사용하여 특정 문화권 double을 변환하는 방법은 무엇입니까? - 라스무스 페이버 How to convert culture specific double using TypeConverter?

+1

하나 현재의 문화에 두 단어, 원하는 문화, 당신은 문자열을 구문 분석하는 데 사용하는 코드를 작동하며 NumberFormatInfo를 어떻게 보았습니까? –

+0

작지만 완전한 코드 조각으로 게시하십시오. –

+0

사실 저는 LINQPAD4에서 작은 콘솔 응용 프로그램으로 작동합니다. 그러나 그것은 대형 응용 프로그램 자체에서 작동하는 것 같지 않습니다. 보아야 할 것이 있습니까? –

답변

2

NumberFormatInfo을보십시오 : 당신은 오랫동안이 NumberGroupSeparator는 다릅니다으로, 다른 것으로 NumberDecimalSeparator을 변경할 수 있습니다

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

.

수정 : 지정한 NumberFormatInfo도 올바르게 작동합니다.

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); 
관련 문제