2004-05-18 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Tue, 18 May 2004 06:55:42 +0000 (06:55 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Tue, 18 May 2004 06:55:42 +0000 (06:55 -0000)
* SqlDecimal.cs :
  - .ctor(double) initializes the precision as 17. If not sufficient,
    it calls AdjustScale(), and if too much then it calls Round().
  - Data should not return the internal array otherwise it might be
    modified by outer code.
  - Reimplemented AdjustScale() to work fine.
  - Reimplemented Parse(). It should rather use Decimal.Parse()
    rather than SqlDouble.Parse() for parsable value range difference.
  - Reimplemented Round(). Don't use double.
  - Fixed ToString() to return "Null" for null value.
  - Fixed explicit let operator to use double .ctor, not decimal
    .ctor() so that precision can be handled in the same way as double.

svn path=/trunk/mcs/; revision=27568

mcs/class/System.Data/System.Data.SqlTypes/ChangeLog
mcs/class/System.Data/System.Data.SqlTypes/SqlDecimal.cs

index 5c9d28a436ec08a1a95f4d43b56256ccf839ff87..23f856bec8ebc79357a39edc4eed029a1346f0e9 100644 (file)
@@ -1,3 +1,18 @@
+2004-05-18  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * SqlDecimal.cs :
+         - .ctor(double) initializes the precision as 17. If not sufficient,
+           it calls AdjustScale(), and if too much then it calls Round().
+         - Data should not return the internal array otherwise it might be
+           modified by outer code.
+         - Reimplemented AdjustScale() to work fine.
+         - Reimplemented Parse(). It should rather use Decimal.Parse() 
+           rather than SqlDouble.Parse() for parsable value range difference.
+         - Reimplemented Round(). Don't use double.
+         - Fixed ToString() to return "Null" for null value.
+         - Fixed explicit let operator to use double .ctor, not decimal
+           .ctor() so that precision can be handled in the same way as double.
+
 2004-05-17  Atsushi Enomoto  <atsushi@ximian.com>
 
        * SqlBinary.cs,
index eb17520c40854b2a39b570ff95856a100ed2356a..0af29a0106eca2f8ccd5ed054b0ae47893d3dfb2 100644 (file)
@@ -90,7 +90,20 @@ namespace System.Data.SqlTypes
                        precision = GetPrecision (value);
                }
                                
-               public SqlDecimal (double value) : this ((decimal)value) { }
+               public SqlDecimal (double value) : this ((decimal)value) 
+               {
+                       SqlDecimal n = this;
+                       int digits = 17 - precision;
+                       if (digits > 0)
+                               n = AdjustScale (this, digits, false);
+                       else
+                               n = Round (this, 17);
+                       this.notNull = n.notNull;
+                       this.positive = n.positive;
+                       this.precision = n.precision;
+                       this.scale = n.scale;
+                       this.value = n.value;
+               }
                public SqlDecimal (int value) : this ((decimal)value) { }
                public SqlDecimal (long value) : this ((decimal)value) { }
 
@@ -142,8 +155,12 @@ namespace System.Data.SqlTypes
                        get { 
                                if (this.IsNull)
                                        throw new SqlNullValueException ();
-                               else
-                                       return (value);
+                               // Data should always return clone, not to be modified
+                               int [] ret = new int [4];
+                               ret [0] = value [0];
+                               ret [1] = value [1];
+                               ret [2] = value [2];
+                               return ret;
                        }
                }
 
@@ -197,15 +214,26 @@ namespace System.Data.SqlTypes
                        if (n.IsNull)
                                throw new SqlNullValueException ();
 
-                       if (digits > 0)
+                       int [] data;
+                       if (digits > 0) {
                                prec = (byte)(prec + digits);
-
-                       if (fRound)
-                               n = Round (n, digits + n.Scale);
+                               decimal d = n.Value;
+                               if (digits > 0)
+                                       for (int i = 0; i < digits; i++)
+                                               d *= 10;
+                               data = Decimal.GetBits (d);
+                               data [3] = 0;
+                       } else {
+                               if (fRound)
+                                       n = Round (n, digits + n.scale);
+                               else
+                                       n = Truncate (n, digits + n.scale);
+                               data = n.Data;
+                       }
 
                        return new SqlDecimal (prec, 
                                               (byte)(n.Scale + digits), 
-                                              n.IsPositive, n.Data);
+                                              n.IsPositive, data);
                }
 
                public static SqlDecimal Ceiling (SqlDecimal n)
@@ -313,7 +341,7 @@ namespace System.Data.SqlTypes
                        if (s == null)
                                throw new ArgumentNullException (Locale.GetText ("string s"));
                        else 
-                               return SqlDouble.Parse (s).ToSqlDecimal ();
+                               return new SqlDecimal (Decimal.Parse (s));
                }
 
                public static SqlDecimal Power (SqlDecimal n, double exp)
@@ -329,12 +357,9 @@ namespace System.Data.SqlTypes
                        if (n.IsNull)
                                throw new SqlNullValueException ();
 
-                       SqlDecimal result = new SqlDecimal (Math.Round (
-                               (double)(n.ToDouble () * Math.Pow (10, position))));
-
-                       result = result / new SqlDecimal(Math.Pow (10, position));
-                       
-                       return result;                          
+                       decimal d = n.Value;
+                       d = Decimal.Round (d, position);
+                       return new SqlDecimal (d);
                }
 
                public static SqlInt32 Sign (SqlDecimal n)
@@ -428,7 +453,7 @@ namespace System.Data.SqlTypes
                public override string ToString ()
                {
                        if (this.IsNull)
-                               return String.Empty;
+                               return "Null";
                        
                        // convert int [4] --> ulong [2]
                        ulong lo = (uint)this.Data [0];
@@ -1249,7 +1274,7 @@ namespace System.Data.SqlTypes
                                if (x.IsNull) 
                                        return Null;
                                else
-                                       return new SqlDecimal ((decimal)x.Value);
+                                       return new SqlDecimal ((double)x.Value);
                        }
                }
 
@@ -1259,7 +1284,7 @@ namespace System.Data.SqlTypes
                                if (x.IsNull) 
                                        return Null;
                                else
-                                       return new SqlDecimal ((decimal)x.Value);
+                                       return new SqlDecimal ((double)x.Value);
                        }
                }