Added files and folders for TARGET_JVM code base
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes.jvm / SqlDecimal.cs
1 /*\r
2 * Copyright (c) 2002-2004 Mainsoft Corporation.\r
3 *\r
4 * Permission is hereby granted, free of charge, to any person obtaining a\r
5 * copy of this software and associated documentation files (the "Software"),\r
6 * to deal in the Software without restriction, including without limitation\r
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
8 * and/or sell copies of the Software, and to permit persons to whom the\r
9 * Software is furnished to do so, subject to the following conditions:\r
10 *\r
11 * The above copyright notice and this permission notice shall be included in\r
12 * all copies or substantial portions of the Software.\r
13 *\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\r
20 * DEALINGS IN THE SOFTWARE.\r
21 */\r
22 \r
23 namespace System.Data.SqlTypes\r
24 {\r
25 \r
26     /**\r
27      * <p>Title: </p>\r
28      * <p>Description: </p>\r
29      * <p>Copyright: Copyright (c) 2002</p>\r
30      * <p>Company: MainSoft</p>\r
31      * @author Pavel Sandler\r
32      * @version 1.0\r
33      */\r
34 \r
35     using System;\r
36 \r
37     using java.math;\r
38 \r
39     /*\r
40     * CURRENT LIMITATIONS:\r
41     * 1. public byte[] Data not implemented.\r
42     * 2. public byte[] BinData not implemented.\r
43     * 3. Precision value is ignored.\r
44     * 4. public SqlDecimal AdjustScale(SqlDecimal n, int position) not implemented.\r
45     * 5. public SqlDecimal ConvertToPrecScale(SqlDecimal n, int precision, int scale) not implemented.\r
46     */\r
47 \r
48 \r
49     public struct SqlDecimal : INullable\r
50     {\r
51 \r
52         private Decimal _value;\r
53         private bool _isNull;\r
54 \r
55         public static readonly SqlDecimal MaxValue = new SqlDecimal(Decimal.MaxValue);\r
56         public static readonly SqlDecimal MinValue = new SqlDecimal(Decimal.MinValue);\r
57         public static readonly int MaxPrecision = 38;\r
58         public static readonly int MaxScale = MaxPrecision;\r
59         public static readonly SqlDecimal Null = new SqlDecimal(true);\r
60 \r
61         private int _precision;\r
62         private int _scale;\r
63         \r
64 \r
65         private SqlDecimal(bool isNull)\r
66         {\r
67             _value = Decimal.Zero;\r
68             _isNull = isNull;\r
69             _precision = 38;\r
70             _scale = 0;\r
71         }\r
72         /**\r
73          * Initializes a new instance of the SqlDecimal instance using the supplied Decimal value.\r
74          * @param value The Decimal value to be stored as a SqlDecimal instance.\r
75          */\r
76         public SqlDecimal(Decimal value) \r
77         {\r
78             _value = value;\r
79             _isNull = false;\r
80             int[] bits = Decimal.GetBits(value);\r
81             int i = bits[3] & 0xff0000;\r
82             _scale = i >> 16;\r
83             _precision = 38;\r
84         }\r
85 \r
86         /**\r
87          * Initializes a new instance of the SqlDecimal instance using the supplied double value.\r
88          * @param value The double value to be stored as a SqlDecimal instance.\r
89          */\r
90         public SqlDecimal(double value) \r
91         { \r
92             _value = new Decimal(value);\r
93             _isNull = false; \r
94             int[] bits = Decimal.GetBits(_value);\r
95             int i = bits[3] & 0xff0000;\r
96             _scale = i >> 16;\r
97             _precision = 38;\r
98         }\r
99 \r
100         /**\r
101          * Initializes a new instance of the SqlDecimal instance using the supplied int value.\r
102          * @param value The int value to be stored as a SqlDecimal instance.\r
103          */\r
104         public SqlDecimal(int value) \r
105         {\r
106             _value = new Decimal(value);\r
107             _isNull = false;\r
108             int[] bits = Decimal.GetBits(_value);\r
109             int i = bits[3] & 0xff0000;\r
110             _scale = i >> 16;\r
111             _precision = 38;\r
112         }\r
113 \r
114         /**\r
115          * Initializes a new instance of the SqlDecimal instance using the supplied long value.\r
116          * @param value The long value to be stored as a SqlDecimal instance.\r
117          */\r
118         public SqlDecimal(long value) \r
119         {\r
120             _value = new Decimal(value);\r
121             _isNull = false;\r
122             int[] bits = Decimal.GetBits(_value);\r
123             int i = bits[3] & 0xff0000;\r
124             _scale = i >> 16;\r
125             _precision = 38;\r
126         }\r
127 \r
128         \r
129         /**\r
130          * Indicates whether or not Value is null.\r
131          * @return true if Value is null, otherwise false.\r
132          */\r
133         public bool IsNull\r
134         {\r
135             get\r
136             {\r
137                 return _isNull;\r
138             }\r
139         }\r
140 \r
141         /**\r
142          * Gets the value of the SqlDecimal instance.\r
143          * @return the value of this instance\r
144          */\r
145         public Decimal Value\r
146         {\r
147             get\r
148             {\r
149                 if(IsNull)\r
150                     throw new SqlNullValueException();\r
151                 return _value;\r
152             }\r
153         }\r
154 \r
155         public byte[] BinData\r
156         {\r
157             get\r
158             {\r
159                 /** @todo implement this method */\r
160                 throw new NotImplementedException();\r
161             }\r
162         }\r
163 \r
164         public byte[] Data\r
165         {\r
166             get\r
167             {\r
168                 /** @todo implement this method */\r
169                 throw new NotImplementedException();\r
170             }\r
171         }\r
172 \r
173         /**\r
174          * Indicates whether or not the Value of this SqlDecimal instance is greater than zero.\r
175          * @return true if the Value is assigned to null, otherwise false.\r
176          */\r
177         public bool IsPositive\r
178         {\r
179             get\r
180             {\r
181                 if (!IsNull)\r
182                 {\r
183                     if (_value >= 0)\r
184                         return true;\r
185 \r
186                     return false;\r
187                 }\r
188             \r
189                 throw new SqlNullValueException("The value of this instance is null");\r
190             }\r
191         }\r
192 \r
193         /**\r
194          * Gets the maximum number of digits used to represent the Value property.\r
195          * @return The maximum number of digits used to represent the Value of this SqlDecimal instance.\r
196          */\r
197         public int Precision\r
198         {\r
199             get\r
200             {\r
201                 return _precision;\r
202             }\r
203         }\r
204 \r
205         /**\r
206          * Gets the number of decimal places to which Value is resolved.\r
207          * @return The number of decimal places to which the Value property is resolved.\r
208          */\r
209         public int Scale\r
210         {\r
211             get\r
212             {\r
213                 return  _precision;\r
214             }\r
215         }\r
216 \r
217         /**\r
218          * The Abs member function gets the absolute value of the SqlDecimal parameter.\r
219          * @param n A SqlDecimal instance.\r
220          * @return A SqlDecimal instance whose Value property contains the unsigned number representing the absolute value of the SqlDecimal parameter.\r
221          */\r
222         public static SqlDecimal Abs(SqlDecimal n)\r
223         {\r
224             if (n.IsNull)\r
225                 return new SqlDecimal();\r
226 \r
227             Decimal val;\r
228 \r
229             if (n.IsPositive)\r
230                 val = n.Value;\r
231             else\r
232                 val = Decimal.Negate(n._value);\r
233 \r
234             return new SqlDecimal(val);\r
235 \r
236         }\r
237 \r
238         /**\r
239          * Calcuates the sum of the two SqlDecimal operators.\r
240          * @param x A SqlDecimal instance.\r
241          * @param y A SqlDecimal instance.\r
242          * @return A new SqlDecimal instance whose Value property contains the sum.\r
243          * If one of the parameters or their value is null return SqlDecimal.Null.\r
244          */\r
245         public static SqlDecimal Add(SqlDecimal x, SqlDecimal y)\r
246         {\r
247             if (x.IsNull || y.IsNull)\r
248                 return SqlDecimal.Null;\r
249 \r
250             Decimal res = Decimal.Add(x._value, y._value);\r
251 \r
252             return new SqlDecimal(res);\r
253         }\r
254 \r
255         public static SqlDecimal AdjustScale(SqlDecimal n, int digits, bool fround)\r
256         {\r
257             /** @todo find out what the logic */\r
258             throw new NotImplementedException();\r
259         }\r
260 \r
261         /**\r
262          * Returns the smallest whole number greater than or equal to the specified SqlDecimal instance.\r
263          * @param n The SqlDecimal instance for which the ceiling value is to be calculated.\r
264          * @return A SqlDecimal representing the smallest whole number greater than or equal to the specified SqlDecimal instance.\r
265          */\r
266         public static SqlDecimal Ceiling(SqlDecimal n)\r
267         {\r
268             if (n.IsNull)\r
269                 return SqlDecimal.Null;\r
270 \r
271             double d = Math.Ceiling((double)n._value);\r
272             return new SqlDecimal(d);\r
273         }\r
274 \r
275         /**\r
276          * Compares this instance to the supplied object and returns an indication of their relative values.\r
277          * @param obj The object to compare.\r
278          * @return A signed number indicating the relative values of the instance and the object.\r
279          * Less than zero This instance is less than object.\r
280          * Zero This instance is the same as object.\r
281          * Greater than zero This instance is greater than object -or-\r
282          * object is a null reference.\r
283          */\r
284         public int CompareTo(Object obj)\r
285         {\r
286             if (obj == null)\r
287                 return 1;\r
288 \r
289             if (obj is SqlDecimal)\r
290             {\r
291                 SqlDecimal value = (SqlDecimal)obj;\r
292                 \r
293                 if (IsNull)\r
294                     return -1;\r
295 \r
296                 if (value.IsNull)\r
297                     return 1;\r
298 \r
299                 if (_value == value._value)\r
300                     return 0;\r
301 \r
302                 return Decimal.Compare(_value, value._value);\r
303             }\r
304 \r
305             throw new ArgumentException("parameter obj is not SqlDecimal : " + obj.GetType().Name);\r
306 \r
307 \r
308         }\r
309 \r
310 \r
311         public SqlDecimal ConvertToPrecScale(SqlDecimal n, int precision, int scale)\r
312         {\r
313             /** @todo find out what the logic */\r
314             throw new NotImplementedException();\r
315         }\r
316 \r
317         /**\r
318          * The division operator divides the first SqlDecimal operand by the second.\r
319          * @param x A SqlDecimal instance.\r
320          * @param y A SqlDecimal instance.\r
321          * @return A SqlDecimal instance containing the results of the division operation.\r
322          * If one of the parameters is null or null value - return SqlDouble.Null.\r
323          */\r
324         public static SqlDecimal Divide(SqlDecimal x, SqlDecimal y)\r
325         {\r
326             if (x.IsNull || y.IsNull)\r
327                 return SqlDecimal.Null;\r
328 \r
329             Decimal res = Decimal.Divide(x._value, y._value);\r
330 \r
331             return new SqlDecimal(res);\r
332         }\r
333 \r
334         public override bool Equals(Object obj)\r
335         {\r
336             if (obj == null)\r
337                 return false;\r
338 \r
339             if (obj is SqlDecimal)\r
340             {\r
341                 SqlDecimal dec = (SqlDecimal)obj;\r
342 \r
343                 return Decimal.Equals(_value, dec._value);\r
344             }\r
345 \r
346             return false;\r
347         }\r
348 \r
349         \r
350         /**\r
351          * Performs a logical comparison on two instances of SqlDecimal to determine if they are equal.\r
352          * @param x A SqlDecimal instance.\r
353          * @param y A SqlDecimal instance.\r
354          * @return true if the two values are equal, otherwise false.\r
355          * If one of the parameters is null or null value return SqlBoolean.Null.\r
356          */\r
357         public static SqlBoolean Equals(SqlDecimal x, SqlDecimal y)\r
358         {\r
359             if (x.IsNull || y.IsNull)\r
360                 return SqlBoolean.Null;\r
361 \r
362             if (x.Equals(y))\r
363                 return SqlBoolean.True;\r
364 \r
365             return SqlBoolean.False;\r
366         }\r
367 \r
368         /**\r
369          * Rounds a specified SqlDecimal number to the next lower whole number.\r
370          * @param n The SqlDecimal instance for which the floor value is to be calculated.\r
371          * @return A SqlDecimal instance containing the whole number portion of this SqlDecimal instance.\r
372          */\r
373         public static SqlDecimal Floor(SqlDecimal n)\r
374         {\r
375             Decimal res = Decimal.Floor(n._value);\r
376 \r
377             return new SqlDecimal(res);\r
378         }\r
379 \r
380         /**\r
381          * Compares two instances of SqlDecimal to determine if the first is greater than the second.\r
382          * @param x A SqlDecimal instance\r
383          * @param y A SqlDecimal instance\r
384          * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.\r
385          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
386          */\r
387         public static SqlBoolean GreaterThan(SqlDecimal x, SqlDecimal y)\r
388         {\r
389             if (x.IsNull || y.IsNull)\r
390                 return SqlBoolean.Null;\r
391 \r
392             if (x.CompareTo(y) > 0)\r
393                 return SqlBoolean.True;\r
394 \r
395             return SqlBoolean.False;\r
396         }\r
397 \r
398         /**\r
399          * Compares two instances of SqlDecimal to determine if the first is greater than or equal to the second.\r
400          * @param x A SqlDecimal instance\r
401          * @param y A SqlDecimal instance\r
402          * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.\r
403          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
404          */\r
405         public static SqlBoolean GreaterThanOrEqual(SqlDecimal x, SqlDecimal y)\r
406         {\r
407             if (x.IsNull || y.IsNull)\r
408                 return SqlBoolean.Null;\r
409 \r
410             if (x.CompareTo(y) >= 0)\r
411                 return SqlBoolean.True;\r
412 \r
413             return SqlBoolean.False;\r
414         }\r
415 \r
416         /**\r
417          * Compares two instances of SqlDecimal to determine if the first is less than the second.\r
418          * @param x A SqlDecimal instance\r
419          * @param y A SqlDecimal instance\r
420          * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.\r
421          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
422          */\r
423         public static SqlBoolean LessThan(SqlDecimal x, SqlDecimal y)\r
424         {\r
425             if (x.IsNull || y.IsNull)\r
426                 return SqlBoolean.Null;\r
427 \r
428             if (x.CompareTo(y) < 0)\r
429                 return SqlBoolean.True;\r
430 \r
431             return SqlBoolean.False;\r
432         }\r
433 \r
434         /**\r
435          * Compares two instances of SqlDecimal to determine if the first is less than the second.\r
436          * @param x A SqlDecimal instance\r
437          * @param y A SqlDecimal instance\r
438          * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.\r
439          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
440          */\r
441         public static SqlBoolean LessThanOrEqual(SqlDecimal x, SqlDecimal y)\r
442         {\r
443             if (x.IsNull || y.IsNull)\r
444                 return SqlBoolean.Null;\r
445 \r
446             if (x.CompareTo(y) <= 0)\r
447                 return SqlBoolean.True;\r
448 \r
449             return SqlBoolean.False;\r
450         }\r
451 \r
452         /**\r
453          * The multiplication operator computes the product of the two SqlDecimal operands.\r
454          * @param x A SqlDecimal instance\r
455          * @param y A SqlDecimal instance\r
456          * @return The product of the two SqlDecimal operands.\r
457          */\r
458         public static SqlDecimal Multiply(SqlDecimal x, SqlDecimal y)\r
459         {\r
460             if (x.IsNull || y.IsNull)\r
461                 return SqlDecimal.Null;\r
462 \r
463             Decimal res = Decimal.Multiply(x._value, y._value);\r
464 \r
465             return new SqlDecimal(res);\r
466         }\r
467 \r
468         /**\r
469          * Compares two instances of SqlDecimal to determine if they are equal.\r
470          * @param x A SqlDecimal instance\r
471          * @param y A SqlDecimal instance\r
472          * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.\r
473          * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.\r
474          */\r
475         public static SqlBoolean NotEquals(SqlDecimal x, SqlDecimal y)\r
476         {\r
477             SqlBoolean eVal = Equals(x, y);\r
478 \r
479             if (eVal.IsNull)\r
480                 return eVal;\r
481             if (eVal.IsTrue)\r
482                 return SqlBoolean.False;\r
483 \r
484             return SqlBoolean.True;\r
485         }\r
486 \r
487         /**\r
488          * Converts the String representation of a number to its Decimal number equivalent.\r
489          * @param s The String to be parsed.\r
490          * @return A SqlDecimal containing the value represented by the String.\r
491          */\r
492         public static SqlDecimal Parse(String s)\r
493         {\r
494             Decimal val = Decimal.Parse(s);\r
495             SqlDecimal retVal = new SqlDecimal(val);\r
496 \r
497             if (GreaterThan(retVal, MaxValue).IsTrue || LessThan(retVal, MinValue).IsTrue)\r
498                 throw new OverflowException("The parse of this string is overflowing : " + val);\r
499 \r
500             return retVal;\r
501 \r
502         }\r
503 \r
504         /**\r
505          * Raises the value of the specified SqlDecimal instance to the specified exponential power.\r
506          * @param n The SqlDecimal instance to be raised to a power.\r
507          * @param exponent A double value indicating the power to which the number should be raised.\r
508          * @return A SqlDecimal instance containing the results.\r
509          */\r
510         public static SqlDecimal Power(SqlDecimal n, double exponent)\r
511         {\r
512             /** @todo decide if we treat the Decimal as a double and use Math.pow() */\r
513             \r
514             double d = (double)n._value;\r
515 \r
516             d = java.lang.Math.pow(d, exponent);\r
517 \r
518             return new SqlDecimal(d);\r
519         }\r
520 \r
521         /**\r
522          * Gets the number nearest the specified SqlDecimal instance's value with the specified precision.\r
523          * @param n The SqlDecimal instance to be rounded.\r
524          * @param position The number of significant fractional digits (precision) in the return value.\r
525          * @return A SqlDecimal instance containing the results of the rounding operation.\r
526          */\r
527         public static SqlDecimal Round(SqlDecimal n, int position)\r
528         {\r
529             Decimal val = Decimal.Round(n._value, position);\r
530 \r
531             return new SqlDecimal(val);\r
532         }\r
533 \r
534         /**\r
535          * Gets a value indicating the sign of a SqlDecimal instance's Value property.\r
536          * @param n The SqlDecimal instance whose sign is to be evaluated.\r
537          * @return A number indicating the sign of the SqlDecimal instance.\r
538          */\r
539         public static int Sign(SqlDecimal n)\r
540         {\r
541             if (n._value < 0)\r
542                 return -1;\r
543             if(n._value > 0)\r
544                 return 1;\r
545             return 0;\r
546         }\r
547 \r
548         /**\r
549          * The subtraction operator the second SqlDecimal operand from the first.\r
550          * @param x A SqlDecimal instance\r
551          * @param y A SqlDecimal instance\r
552          * @return The results of the subtraction operation.\r
553          */\r
554         public static SqlDecimal Subtract(SqlDecimal x, SqlDecimal y)\r
555         {\r
556             Decimal val = Decimal.Subtract(x._value, y._value);\r
557             SqlDecimal retVal = new SqlDecimal(val);\r
558 \r
559             return retVal;\r
560 \r
561         }\r
562 \r
563         /**\r
564          * Returns the a double equal to the contents of the Value property of this instance.\r
565          * @return The decimal representation of the Value property.\r
566          */\r
567         public double ToDouble()\r
568         {\r
569             return Decimal.ToDouble(_value);\r
570         }\r
571 \r
572         /**\r
573          * Converts this SqlDecimal instance to SqlBoolean.\r
574          * @return A SqlBoolean instance whose Value will be True if the SqlDecimal instance's Value is non-zero,\r
575          * False if the SqlDecimal is zero\r
576          * and Null if the SqlDecimal instance is Null.\r
577          */\r
578         public SqlBoolean ToSqlBoolean()\r
579         {\r
580             if (IsNull)\r
581                 return SqlBoolean.Null;\r
582 \r
583             return new SqlBoolean(!_value.Equals(Decimal.Zero));\r
584         }\r
585 \r
586         /**\r
587          * Converts this SqlDecimal instance to SqlByte.\r
588          * @return A SqlByte instance whose Value equals the Value of this SqlDouble instance.\r
589          */\r
590         public SqlByte ToSqlByte()\r
591         {\r
592             if (IsNull)\r
593                 return SqlByte.Null;\r
594 \r
595             return new SqlByte(checked((byte)_value));\r
596         }\r
597 \r
598         /**\r
599          * Converts this SqlDecimal instance to SqlDouble.\r
600          * @return A SqlDouble instance whose Value equals the Value of this SqlDecimal instance.\r
601          */\r
602         public SqlDouble ToSqlDouble()\r
603         {\r
604             if (IsNull)\r
605                 return SqlDouble.Null;\r
606 \r
607             return new SqlDouble((double)_value);\r
608         }\r
609 \r
610         /**\r
611          * Converts this SqlDouble structure to SqlInt16.\r
612          * @return A SqlInt16 structure whose Value equals the Value of this SqlDouble structure.\r
613          */\r
614         public SqlInt16 ToSqlInt16()\r
615         {\r
616             if (IsNull)\r
617                 return SqlInt16.Null;\r
618 \r
619             return new SqlInt16(checked((short)_value));\r
620         }\r
621 \r
622         /**\r
623          * Converts this SqlDouble structure to SqlInt32.\r
624          * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.\r
625          */\r
626         public SqlInt32 ToSqlInt32()\r
627         {\r
628             if (IsNull)\r
629                 return SqlInt32.Null;\r
630 \r
631             return new SqlInt32(checked((int)_value));\r
632         }\r
633 \r
634         /**\r
635          * Converts this SqlDecimal structure to SqlInt64.\r
636          * @return A SqlInt64 structure whose Value equals the Value of this SqlDecimal structure.\r
637          */\r
638         public SqlInt64 ToSqlInt64()\r
639         {\r
640             if (IsNull)\r
641                 return SqlInt64.Null;\r
642 \r
643             return new SqlInt64(checked((long)_value));\r
644         }\r
645 \r
646         /**\r
647          * Converts this SqlDecimal instance to SqlDouble.\r
648          * @return A SqlMoney instance whose Value equals the Value of this SqlDecimal instance.\r
649          */\r
650         public SqlMoney ToSqlMoney()\r
651         {\r
652             if (IsNull)\r
653                 return SqlMoney.Null;\r
654 \r
655             return new SqlMoney(_value);\r
656         }\r
657 \r
658         /**\r
659          * Converts this SqlDecimal instance to SqlSingle.\r
660          * @return A SqlSingle instance whose Value equals the Value of this SqlDecimal instance.\r
661          */\r
662         public SqlSingle ToSqlSingle()\r
663         {\r
664             if (IsNull)\r
665                 return SqlSingle.Null;\r
666 \r
667             return new SqlSingle(checked((float)_value));\r
668         }\r
669 \r
670         /**\r
671          * Converts this SqlDecimal structure to SqlString.\r
672          * @return A SqlString structure whose value is a string representing the date and time contained in this SqlDecimal structure.\r
673          */\r
674         public SqlString ToSqlString()\r
675         {\r
676             return new SqlString(ToString());\r
677         }\r
678 \r
679 \r
680         public override String ToString()\r
681         {\r
682             if (IsNull)\r
683                 return "null";\r
684 \r
685             return _value.ToString();\r
686         }\r
687 \r
688         /**\r
689          * Truncates the specified SqlDecimal instance's value to the desired position.\r
690          * @param n The SqlDecimal instance to be truncated.\r
691          * @param position The decimal position to which the number will be truncated.\r
692          * @return Supply a negative value for the position parameter in order to truncate the value to the corresponding positon to the left of the decimal point.\r
693          */\r
694         public static SqlDecimal Truncate(SqlDecimal n, int position)\r
695         {\r
696             if (n.IsNull)\r
697                 return n;\r
698             \r
699             Decimal tmp = Decimal.Round(n._value, position);\r
700 \r
701             return new SqlDecimal(tmp);\r
702         }\r
703 \r
704         public override int GetHashCode()\r
705         {\r
706             return _value.GetHashCode();\r
707         }\r
708         \r
709         public static SqlDecimal operator + (SqlDecimal x, SqlDecimal y)\r
710         {\r
711             if(x.IsNull || y.IsNull)\r
712                 return SqlDecimal.Null;\r
713 \r
714             return new SqlDecimal(x.Value + y.Value);\r
715         }\r
716 \r
717         public static SqlDecimal operator / (SqlDecimal x, SqlDecimal y)\r
718         {\r
719             if(x.IsNull || y.IsNull)\r
720                 return SqlDecimal.Null;\r
721             return new SqlDecimal (x.Value / y.Value);\r
722         }\r
723 \r
724         public static SqlBoolean operator == (SqlDecimal x, SqlDecimal y)\r
725         {\r
726             if (x.IsNull || y.IsNull) \r
727                 return SqlBoolean.Null;\r
728 \r
729             return new SqlBoolean(x.Value == y.Value);\r
730         }\r
731 \r
732         public static SqlBoolean operator > (SqlDecimal x, SqlDecimal y)\r
733         {\r
734             if (x.IsNull || y.IsNull) \r
735                 return SqlBoolean.Null;\r
736 \r
737             return new SqlBoolean(x.Value > y.Value);\r
738         }\r
739 \r
740         public static SqlBoolean operator >= (SqlDecimal x, SqlDecimal y)\r
741         {\r
742             if (x.IsNull || y.IsNull) \r
743                 return SqlBoolean.Null;\r
744 \r
745             return new SqlBoolean(x.Value >= y.Value);\r
746         }\r
747 \r
748         public static SqlBoolean operator != (SqlDecimal x, SqlDecimal y)\r
749         {\r
750             if (x.IsNull || y.IsNull) \r
751                 return SqlBoolean.Null;\r
752 \r
753             return new SqlBoolean(x.Value != y.Value);\r
754         }\r
755 \r
756         public static SqlBoolean operator < (SqlDecimal x, SqlDecimal y)\r
757         {\r
758 \r
759             if (x.IsNull || y.IsNull) \r
760                 return SqlBoolean.Null;\r
761 \r
762             return new SqlBoolean(x.Value < y.Value);\r
763 \r
764         }\r
765 \r
766         public static SqlBoolean operator <= (SqlDecimal x, SqlDecimal y)\r
767         {\r
768             if (x.IsNull || y.IsNull) \r
769                 return SqlBoolean.Null;\r
770 \r
771             return new SqlBoolean(x.Value <= y.Value);\r
772         }\r
773 \r
774         public static SqlDecimal operator * (SqlDecimal x, SqlDecimal y)\r
775         {\r
776             // adjust the scale to the smaller of the two beforehand\r
777             if (x.Scale > y.Scale)\r
778                 x = SqlDecimal.AdjustScale(x, y.Scale - x.Scale, true);\r
779             else if (y.Scale > x.Scale)\r
780                 y = SqlDecimal.AdjustScale(y, x.Scale - y.Scale, true);\r
781 \r
782             return new SqlDecimal(x.Value * y.Value);\r
783         }\r
784 \r
785         public static SqlDecimal operator - (SqlDecimal x, SqlDecimal y)\r
786         {\r
787             if(x.IsNull || y.IsNull)\r
788                 return SqlDecimal.Null;\r
789 \r
790             return new SqlDecimal(x.Value - y.Value);\r
791         }\r
792 \r
793         public static SqlDecimal operator - (SqlDecimal n)\r
794         {\r
795             if(n.IsNull)\r
796                 return n;\r
797             return new SqlDecimal (Decimal.Negate(n.Value));\r
798         }\r
799 \r
800         public static explicit operator SqlDecimal (SqlBoolean x)\r
801         {\r
802             if (x.IsNull) \r
803                 return Null;\r
804             else\r
805                 return new SqlDecimal ((decimal)x.ByteValue);\r
806         }\r
807 \r
808         public static explicit operator Decimal (SqlDecimal n)\r
809         {\r
810             return n.Value;\r
811         }\r
812 \r
813         public static explicit operator SqlDecimal (SqlDouble x)\r
814         {\r
815             checked \r
816             {\r
817                 if (x.IsNull) \r
818                     return Null;\r
819                 else\r
820                     return new SqlDecimal ((decimal)x.Value);\r
821             }\r
822         }\r
823 \r
824         public static explicit operator SqlDecimal (SqlSingle x)\r
825         {\r
826             checked \r
827             {\r
828                 if (x.IsNull) \r
829                     return Null;\r
830                 else\r
831                     return new SqlDecimal ((decimal)x.Value);\r
832             }\r
833         }\r
834 \r
835         public static explicit operator SqlDecimal (SqlString x)\r
836         {\r
837             checked \r
838             {\r
839                 return Parse (x.Value);\r
840             }\r
841         }\r
842 \r
843         public static implicit operator SqlDecimal (decimal x)\r
844         {\r
845             return new SqlDecimal (x);\r
846         }\r
847 \r
848         public static implicit operator SqlDecimal (SqlByte x)\r
849         {\r
850             if (x.IsNull) \r
851                 return Null;\r
852             else\r
853                 return new SqlDecimal ((decimal)x.Value);\r
854         }\r
855 \r
856         public static implicit operator SqlDecimal (SqlInt16 x)\r
857         {\r
858             if (x.IsNull) \r
859                 return Null;\r
860             else\r
861                 return new SqlDecimal ((decimal)x.Value);\r
862         }\r
863 \r
864         public static implicit operator SqlDecimal (SqlInt32 x)\r
865         {\r
866             if (x.IsNull) \r
867                 return Null;\r
868             else\r
869                 return new SqlDecimal ((decimal)x.Value);\r
870         }\r
871 \r
872         public static implicit operator SqlDecimal (SqlInt64 x)\r
873         {\r
874             if (x.IsNull) \r
875                 return Null;\r
876             else\r
877                 return new SqlDecimal ((decimal)x.Value);\r
878         }\r
879 \r
880         public static implicit operator SqlDecimal (SqlMoney x)\r
881         {\r
882             if (x.IsNull) \r
883                 return Null;\r
884             else\r
885                 return new SqlDecimal ((decimal)x.Value);\r
886         }\r
887 \r
888     }\r
889 }