1 // System.Data.SqlTypes.SqlMoney
\r
4 // Konstantin Triger <kostat@mainsoft.com>
\r
5 // Boris Kirzner <borisk@mainsoft.com>
\r
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
31 namespace System.Data.SqlTypes
\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
47 * CURRENT LIMITATIONS:
\r
51 public struct SqlMoney : INullable
\r
54 private Decimal _value;
\r
55 private bool _isNull;
\r
57 public static readonly SqlMoney MaxValue = new SqlMoney(922337203685475.5807m, true);
\r
58 public static readonly SqlMoney MinValue = new SqlMoney(-922337203685477.5808m, true);
\r
59 public static readonly SqlMoney Null = new SqlMoney(true);
\r
60 public static readonly SqlMoney Zero = new SqlMoney(0);
\r
62 // this ctor is to initailize max and min values, to avoid checks.
\r
63 private SqlMoney(Decimal value, bool dummy)
\r
69 private SqlMoney(bool isNull)
\r
75 * Initializes a new instance of the SqlMoney instance using the supplied Decimal value.
\r
76 * @param value The Decimal value to be stored as a SqlMoney instance.
\r
78 public SqlMoney(Decimal value)
\r
80 if ((value.CompareTo(MaxValue.Value) > 0 || value.CompareTo(MinValue.Value) < 0))
\r
81 throw new OverflowException("overflow - the value is out of range " + value);
\r
89 * Initializes a new instance of the SqlMoney instance using the supplied double value.
\r
90 * @param value The double value to be stored as a SqlMoney instance.
\r
92 public SqlMoney(double value)
\r
94 _value = new Decimal(value);
\r
95 if (_value.CompareTo(MaxValue.Value) > 0 || _value.CompareTo(MinValue.Value) < 0)
\r
96 throw new OverflowException("overflow - the value is out of range " + value);
\r
102 * Initializes a new instance of the SqlMoney instance using the supplied int value.
\r
103 * @param value The int value to be stored as a SqlMoney instance.
\r
105 public SqlMoney(int value)
\r
107 _value = new Decimal(value);
\r
112 * Initializes a new instance of the SqlMoney instance using the supplied long value.
\r
113 * @param value The long value to be stored as a SqlMoney instance.
\r
115 public SqlMoney(long value)
\r
117 _value = new Decimal(value);
\r
118 if (_value.CompareTo(MaxValue.Value) > 0 || _value.CompareTo(MinValue.Value) < 0)
\r
119 throw new OverflowException("overflow - the value is out of range " + value);
\r
125 * Indicates whether or not Value is null.
\r
126 * @return true if Value is null, otherwise false.
\r
137 * Gets the value of the SqlMoney instance.
\r
138 * @return the value of this instance
\r
140 public Decimal Value
\r
145 throw new SqlNullValueException();
\r
151 * Calcuates the sum of the two SqlMoney operators.
\r
152 * @param x A SqlMoney instance.
\r
153 * @param y A SqlMoney instance.
\r
154 * @return A new SqlMoney instance whose Value property contains the sum.
\r
155 * If one of the parameters or their value is null return SqlMoney.Null.
\r
157 public static SqlMoney Add(SqlMoney x, SqlMoney y)
\r
159 if (x.IsNull || y.IsNull)
\r
160 return SqlMoney.Null;
\r
162 Decimal res = Decimal.Add(x._value, y._value);
\r
164 if (res.CompareTo(MaxValue.Value) > 0 || res.CompareTo(MinValue.Value) < 0)
\r
165 throw new OverflowException("overflow - the sum of the 2 parameters can not be SqlMoney : " + res);
\r
167 return new SqlMoney(res);
\r
171 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
172 * @param obj The object to compare.
\r
173 * @return A signed number indicating the relative values of the instance and the object.
\r
174 * Less than zero This instance is less than object.
\r
175 * Zero This instance is the same as object.
\r
176 * Greater than zero This instance is greater than object -or-
\r
177 * object is a null reference.
\r
179 public int CompareTo(Object obj)
\r
184 if (obj is SqlMoney)
\r
186 SqlMoney value = (SqlMoney)obj;
\r
194 return Decimal.Compare(_value, value._value);
\r
197 /** @todo throwArgumentException */
\r
198 throw new ArgumentException("parameter obj is not SqlMoney : " + obj.GetType().Name);
\r
205 * The division operator divides the first SqlMoney operand by the second.
\r
206 * @param x A SqlMoney instance.
\r
207 * @param y A SqlMoney instance.
\r
208 * @return A SqlMoney instance containing the results of the division operation.
\r
209 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
211 public static SqlMoney Divide(SqlMoney x, SqlMoney y)
\r
213 if (x.IsNull || y.IsNull)
\r
214 return SqlMoney.Null;
\r
216 Decimal res = Decimal.Divide(x._value, y._value);
\r
218 return new SqlMoney(res);
\r
221 public override bool Equals(Object obj)
\r
226 if (obj is SqlMoney)
\r
228 SqlMoney dec = (SqlMoney)obj;
\r
230 return Decimal.Equals(_value, dec._value);
\r
238 * Performs a logical comparison on two instances of SqlMoney to determine if they are equal.
\r
239 * @param x A SqlMoney instance.
\r
240 * @param y A SqlMoney instance.
\r
241 * @return true if the two values are equal, otherwise false.
\r
242 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
244 public static SqlBoolean Equals(SqlMoney x, SqlMoney y)
\r
246 if (x.IsNull || y.IsNull)
\r
247 return SqlBoolean.Null;
\r
250 return SqlBoolean.True;
\r
252 return SqlBoolean.False;
\r
257 * Compares two instances of SqlMoney to determine if the first is greater than the second.
\r
258 * @param x A SqlMoney instance
\r
259 * @param y A SqlMoney instance
\r
260 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
261 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
263 public static SqlBoolean GreaterThan(SqlMoney x, SqlMoney y)
\r
265 if (x.IsNull || y.IsNull)
\r
266 return SqlBoolean.Null;
\r
268 if (x.CompareTo(y) > 0)
\r
269 return SqlBoolean.True;
\r
271 return SqlBoolean.False;
\r
275 * Compares two instances of SqlMoney to determine if the first is greater than or equal to the second.
\r
276 * @param x A SqlMoney instance
\r
277 * @param y A SqlMoney instance
\r
278 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
279 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
281 public static SqlBoolean GreaterThanOrEqual(SqlMoney x, SqlMoney y)
\r
283 if (x.IsNull || y.IsNull)
\r
284 return SqlBoolean.Null;
\r
286 if (x.CompareTo(y) >= 0)
\r
287 return SqlBoolean.True;
\r
289 return SqlBoolean.False;
\r
293 * Compares two instances of SqlMoney to determine if the first is less than the second.
\r
294 * @param x A SqlMoney instance
\r
295 * @param y A SqlMoney instance
\r
296 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
297 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
299 public static SqlBoolean LessThan(SqlMoney x, SqlMoney y)
\r
301 if (x.IsNull || y.IsNull)
\r
302 return SqlBoolean.Null;
\r
304 if (x.CompareTo(y) < 0)
\r
305 return SqlBoolean.True;
\r
307 return SqlBoolean.False;
\r
311 * Compares two instances of SqlMoney to determine if the first is less than the second.
\r
312 * @param x A SqlMoney instance
\r
313 * @param y A SqlMoney instance
\r
314 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
315 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
317 public static SqlBoolean LessThanOrEqual(SqlMoney x, SqlMoney y)
\r
319 if (x.IsNull || y.IsNull)
\r
320 return SqlBoolean.Null;
\r
322 if (x.CompareTo(y) <= 0)
\r
323 return SqlBoolean.True;
\r
325 return SqlBoolean.False;
\r
329 * The multiplication operator computes the product of the two SqlMoney operands.
\r
330 * @param x A SqlMoney instance
\r
331 * @param y A SqlMoney instance
\r
332 * @return The product of the two SqlMoney operands.
\r
334 public static SqlMoney Multiply(SqlMoney x, SqlMoney y)
\r
336 if (x.IsNull || y.IsNull)
\r
337 return SqlMoney.Null;
\r
339 Decimal res = Decimal.Multiply(x._value, y._value);
\r
341 if (res.CompareTo(MaxValue.Value) > 0 || res.CompareTo(MinValue.Value) < 0)
\r
342 throw new OverflowException("overflow - the multiply value is out of range " + res);
\r
344 return new SqlMoney(res);
\r
348 * Compares two instances of SqlMoney to determine if they are equal.
\r
349 * @param x A SqlMoney instance
\r
350 * @param y A SqlMoney instance
\r
351 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
352 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
354 public static SqlBoolean NotEquals(SqlMoney x, SqlMoney y)
\r
356 SqlBoolean eVal = Equals(x, y);
\r
361 return SqlBoolean.False;
\r
363 return SqlBoolean.True;
\r
367 * Converts the String representation of a number to its Decimal number equivalent.
\r
368 * @param s The String to be parsed.
\r
369 * @return A SqlMoney containing the value represented by the String.
\r
371 public static SqlMoney Parse(String s)
\r
373 Decimal val = Decimal.Parse(s);
\r
374 SqlMoney retVal = new SqlMoney(val);
\r
376 if (GreaterThan(retVal, MaxValue).IsTrue || LessThan(retVal, MinValue).IsTrue)
\r
377 throw new OverflowException("the value is out of range : " + retVal);
\r
383 * The subtraction operator the second SqlMoney operand from the first.
\r
384 * @param x A SqlMoney instance
\r
385 * @param y A SqlMoney instance
\r
386 * @return The results of the subtraction operation.
\r
388 public static SqlMoney Subtract(SqlMoney x, SqlMoney y)
\r
390 Decimal val = Decimal.Subtract(x._value, y._value);
\r
391 SqlMoney retVal = new SqlMoney(val);
\r
393 if (GreaterThan(retVal, MaxValue).IsTrue || LessThan(retVal, MinValue).IsTrue)
\r
394 throw new OverflowException("the subtract result is out of range : " + retVal);
\r
401 * Returns the a double equal to the contents of the Value property of this instance.
\r
402 * @return The decimal representation of the Value property.
\r
404 public double ToDouble()
\r
406 return Decimal.ToDouble(_value);
\r
410 * Returns the a decimal equal to the contents of the Value property of this instance.
\r
411 * @return The decimal representation of the Value property.
\r
413 public Decimal ToDecimal()
\r
419 * Converts this SqlMoney instance to SqlBoolean.
\r
420 * @return A SqlBoolean instance whose Value will be True if the SqlMoney instance's Value is non-zero,
\r
421 * False if the SqlMoney is zero
\r
422 * and Null if the SqlMoney instance is Null.
\r
424 public SqlBoolean ToSqlBoolean()
\r
427 return SqlBoolean.Null;
\r
429 int val = Decimal.ToInt32(_value);
\r
431 return new SqlBoolean(val);
\r
435 * Converts this SqlMoney instance to SqlByte.
\r
436 * @return A SqlByte instance whose Value equals the Value of this SqlDouble instance.
\r
438 public SqlByte ToSqlByte()
\r
441 return SqlByte.Null;
\r
443 return new SqlByte(Decimal.ToByte(_value));
\r
447 * Converts this SqlMoney instance to SqlDouble.
\r
448 * @return A SqlDouble instance whose Value equals the Value of this SqlMoney instance.
\r
450 public SqlDouble ToSqlDouble()
\r
453 return SqlDouble.Null;
\r
455 return new SqlDouble(Decimal.ToDouble(_value));
\r
459 * Converts this SqlMoney instance to SqlSingle.
\r
460 * @return A SqlSingle instance whose Value equals the Value of this SqlMoney instance.
\r
462 public SqlSingle ToSqlSingle()
\r
465 return SqlSingle.Null;
\r
467 return new SqlSingle(Decimal.ToSingle(_value));
\r
471 * Converts this SqlMoney instance to SqlDecimal.
\r
472 * @return A SqlDecimal instance whose Value equals the Value of this SqlMoney instance.
\r
474 public SqlDecimal ToSqlDecimal()
\r
477 return SqlDecimal.Null;
\r
479 return new SqlDecimal(_value);
\r
484 * Converts this SqlDouble structure to SqlInt16.
\r
485 * @return A SqlInt16 structure whose Value equals the Value of this SqlDouble structure.
\r
487 public SqlInt16 ToSqlInt16()
\r
490 return SqlInt16.Null;
\r
492 return new SqlInt16(Decimal.ToInt16(_value));
\r
496 * Converts this SqlDouble structure to SqlInt32.
\r
497 * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.
\r
499 public SqlInt32 ToSqlInt32()
\r
502 return SqlInt32.Null;
\r
504 return new SqlInt32(Decimal.ToInt32(_value));
\r
508 * Converts this SqlMoney structure to SqlInt64.
\r
509 * @return A SqlInt64 structure whose Value equals the Value of this SqlMoney structure.
\r
511 public SqlInt64 ToSqlInt64()
\r
514 return SqlInt64.Null;
\r
515 return new SqlInt64(Decimal.ToInt64(_value));
\r
520 * Converts this SqlMoney structure to SqlString.
\r
521 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlMoney structure.
\r
523 public SqlString ToSqlString()
\r
526 return SqlString.Null;
\r
527 return new SqlString(ToString());
\r
530 public override String ToString()
\r
534 return _value.ToString();
\r
537 public override int GetHashCode()
\r
541 return _value.GetHashCode();
\r
544 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
\r
548 return new SqlMoney (x.Value + y.Value);
\r
552 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
\r
556 return new SqlMoney (x.Value / y.Value);
\r
560 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
\r
562 if (x.IsNull || y.IsNull)
\r
563 return SqlBoolean.Null;
\r
565 return new SqlBoolean (x.Value == y.Value);
\r
568 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
\r
570 if (x.IsNull || y.IsNull)
\r
571 return SqlBoolean.Null;
\r
573 return new SqlBoolean (x.Value > y.Value);
\r
576 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
\r
578 if (x.IsNull || y.IsNull)
\r
579 return SqlBoolean.Null;
\r
581 return new SqlBoolean (x.Value >= y.Value);
\r
584 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
\r
586 if (x.IsNull || y.IsNull)
\r
587 return SqlBoolean.Null;
\r
589 return new SqlBoolean (!(x.Value == y.Value));
\r
592 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
\r
594 if (x.IsNull || y.IsNull)
\r
595 return SqlBoolean.Null;
\r
597 return new SqlBoolean (x.Value < y.Value);
\r
600 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
\r
602 if (x.IsNull || y.IsNull)
\r
603 return SqlBoolean.Null;
\r
604 return new SqlBoolean (x.Value <= y.Value);
\r
607 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
\r
611 return new SqlMoney (x.Value * y.Value);
\r
615 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
\r
619 return new SqlMoney (x.Value - y.Value);
\r
623 public static SqlMoney operator - (SqlMoney n)
\r
625 return new SqlMoney (-(n.Value));
\r
628 public static explicit operator SqlMoney (SqlBoolean x)
\r
636 return new SqlMoney ((decimal)x.ByteValue);
\r
641 public static explicit operator SqlMoney (SqlDecimal x)
\r
649 return new SqlMoney (x.Value);
\r
654 public static explicit operator SqlMoney (SqlDouble x)
\r
662 return new SqlMoney ((decimal)x.Value);
\r
667 public static explicit operator decimal (SqlMoney x)
\r
672 public static explicit operator SqlMoney (SqlSingle x)
\r
680 return new SqlMoney ((decimal)x.Value);
\r
685 public static explicit operator SqlMoney (SqlString x)
\r
689 return SqlMoney.Parse (x.Value);
\r
693 public static implicit operator SqlMoney (decimal x)
\r
695 return new SqlMoney (x);
\r
698 public static implicit operator SqlMoney (SqlByte x)
\r
703 return new SqlMoney ((decimal)x.Value);
\r
706 public static implicit operator SqlMoney (SqlInt16 x)
\r
711 return new SqlMoney ((decimal)x.Value);
\r
714 public static implicit operator SqlMoney (SqlInt32 x)
\r
719 return new SqlMoney (x.Value);
\r
722 public static implicit operator SqlMoney (SqlInt64 x)
\r
727 return new SqlMoney (x.Value);
\r