[Mono-bugs] [Bug 77721][Nor] New - Error parsing currency as Double

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Tue Mar 7 10:05:49 EST 2006


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by nede at aliquant.com.

http://bugzilla.ximian.com/show_bug.cgi?id=77721

--- shadow/77721	2006-03-07 10:05:49.000000000 -0500
+++ shadow/77721.tmp.12806	2006-03-07 10:05:49.000000000 -0500
@@ -0,0 +1,158 @@
+Bug#: 77721
+Product: Mono: Class Libraries
+Version: 1.1
+OS: All
+OS Details: Fedora Core 4
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: CORLIB
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: nede at aliquant.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Error parsing currency as Double
+
+Description of Problem:
+Parsing a string currency value as a System.Double throws the following error:
+
+Unhandled Exception: System.FormatException: Unknown char: $
+
+This does not occur with MS's .NET Framework.
+
+
+Steps to reproduce the problem:
+Compile and run the following code:
+/* begin C# */
+using System;
+using System.Globalization;
+
+namespace FunConsoleApp
+{
+  class Class1
+  {
+    [STAThread]
+    static void Main(string[] args)
+    {
+      System.Threading.Thread.CurrentThread.CurrentCulture = new
+CultureInfo("en-US");
+      NumberFormatInfo oFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
+      double dMin = double.Parse("$0.00", NumberStyles.Currency, oFormatInfo);
+      Console.WriteLine(dMin.ToString());
+    }
+  }
+}
+/* end C# */
+
+
+Actual Results:
+Unhandled Exception: System.FormatException: Unknown char: $
+in <0x00052> System.Double:Parse (System.String s, NumberStyles style,
+IFormatProvider provider)
+in <0x0005d> FunConsoleApp.Class1:Main (System.String[] args)
+
+
+Expected Results:
+0
+
+
+How often does this happen? 
+Every time.
+
+
+Additional Information:
+I am using revision 57653 of mcs from svn.
+The following code in mcs/class/corlib/System/Double.cs seems to fix the
+problem (starting at line 278):
+
+			//
+			// Setup
+			//
+			string decimal_separator = null;
+			string group_separator = null;
+			string currency_symbol = null;
+			int decimal_separator_len = 0;
+			int group_separator_len = 0;
+			int currency_symbol_len = 0;
+			if ((style & NumberStyles.AllowDecimalPoint) != 0){
+				decimal_separator = format.NumberDecimalSeparator;
+				decimal_separator_len = decimal_separator.Length;
+			}
+			if ((style & NumberStyles.AllowThousands) != 0){
+				group_separator = format.NumberGroupSeparator;
+				group_separator_len = group_separator.Length;
+			}
+			if ((style & NumberStyles.AllowCurrencySymbol) != 0){
+				currency_symbol = format.CurrencySymbol;
+				currency_symbol_len = currency_symbol.Length;
+			}
+			string positive = format.PositiveSign;
+			string negative = format.NegativeSign;
+
+			for (; sidx < len; sidx++){
+				c = s [sidx];
+
+				if (c == '\0') {
+					sidx = len;
+					continue;
+				}
+				switch (state){
+				case State_AllowSign:
+					if ((style & NumberStyles.AllowLeadingSign) != 0){
+						if (c == positive [0] &&
+						    s.Substring (sidx, positive.Length) == positive){
+							state = State_Digits;
+							sidx += positive.Length-1;
+							continue;
+						}
+
+						if (c == negative [0] &&
+						    s.Substring (sidx, negative.Length) == negative){
+							state = State_Digits;
+							b [didx++] = (byte) '-';
+							sidx += negative.Length-1;
+							continue;
+						}
+					}
+					state = State_Digits;
+					goto case State_Digits;
+					
+				case State_Digits:
+					if (Char.IsDigit (c)){
+						b [didx++] = (byte) c;
+						break;
+					}
+					if (c == 'e' || c == 'E')
+						goto case State_Decimal;
+					
+					if (decimal_separator != null &&
+					    decimal_separator [0] == c){
+						if (s.Substring (sidx, decimal_separator_len) ==
+						    decimal_separator){
+							b [didx++] = (byte) '.';
+							sidx += decimal_separator_len-1;
+							state = State_Decimal; 
+							break;
+						}
+					}
+					if (group_separator != null &&
+					    group_separator [0] == c){
+						if (s.Substring (sidx, group_separator_len) ==
+						    group_separator){
+							sidx += group_separator_len-1;
+							state = State_Digits; 
+							break;
+						}
+					}
+					if (currency_symbol != null &&
+					    currency_symbol [0] == c){
+						if (s.Substring (sidx, currency_symbol_len) ==
+						    currency_symbol){
+							sidx += currency_symbol_len-1;
+							state = State_Digits; 
+							break;
+						}
+					}


More information about the mono-bugs mailing list