Softwareentwicklung
›
Thread
C# Convert.todouble komische ausgabe
P
Philipp
Gast
21.06.2013 21:44
#3212158
Hallo,
ich versuche gerade einen String in einen Double zu Konvertieren.
Dazu habe ich von der MSDN Homepage folgendes Beispiel ausprobiert.
http://msdn.microsoft.com/de-de/library/vstudio/zh1hkw6k%28v=vs.100%29.aspx
Leider funktioniert das nicht so wie auf der Seite beschrieben.
1 string [] values = { "-1,035.77219" , "1AFF" , "1e-35" ,
2 "1,635,592,999,999,999,999,999,999" , "-17.455" ,
3 "190.34001" , "1.29e325" };
4 double result ;
5
6 foreach ( string value in values )
7 {
8 try {
9 result = Convert . ToDouble ( value );
10 Console . WriteLine ( "Converted '{0}' to {1}." , value , result );
11 }
12 catch ( FormatException ) {
13 Console . WriteLine ( "Unable to convert '{0}' to a Double." , value );
14 }
15 catch ( OverflowException ) {
16 Console . WriteLine ( "'{0}' is outside the range of a Double." , value );
17 }
18 }
19 // The example displays the following output:
20 // Converted '-1,035.77219' to -1035.77219.
21 // Unable to convert '1AFF' to a Double.
22 // Converted '1e-35' to 1E-35.
23 // Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24.
24 // Converted '-17.455' to -17.455.
25 // Converted '190.34001' to 190.34001.
26 // '1.29e325' is outside the range of a Double.
Wenn ich den Code ausführe kommt folgendes dabei raus:
1 Unable to convert - 1 , 035 . 77219 to a Double
2 Unable to convert 1 AFF to a Double
3 Converted 1e-35 to 1E-35
4 Unable to convert 1 , 635 , 592 , 999 , 999 , 999 , 999 , 999 , 999 to a Double
5 Converted - 17 . 455 to - 17455
6 Converted 190 . 34001 to 19034001
7 1.29e325 is outside the range of a Double
ich verwende Visual Studio 2010 Premium mit .Net Framework 4.0
Muss ich iwo noch ein paar Einstellungen machen damit das ganze richtig
funktioniert?
Beziehungsweiße würde es mich interessieren, was beu euch rauskommt wenn
ihr das ganze Beispiel laufen lasst??
Gruß Philipp
PI
Peter II
Gast
21.06.2013 21:47
#3212162
das Problem ist das c# so "schlau" ist, die Spracheinstellung dafür zu
verwenden. Und damit tauscht der . und das , die Bedeutung. jetzt musst
du erst mal überlegen was wie du es brauchst.
S
SEsch
Gast
21.06.2013 21:58
#3212182
Schau mal hier:
http://msdn.microsoft.com/de-de/library/vstudio/9s9ak971.aspx
evtl. genügt dir auch 1 using System.Globalization;
2
3 ...
4
5 result = Convert.ToDouble(value, CultureInfo.InvariantCulture);
P
Philipp
Gast
21.06.2013 22:19
#3212211
HAHA C# "schlau" ;) das gefällt mir...
@ SEsch danke für deinen Tipp
Funktioniert jetzt prima
UU
Uhu U.
(uhu)
21.06.2013 22:38
#3212227
C# ist der Cobol-Dialekt von C - man schreibt sich dusselig...
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren