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