2 // System.Data.SqlTypes.SqlMoney
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlMoney : INullable, IComparable
47 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685477.5807m);
48 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808m);
49 public static readonly SqlMoney Null;
50 public static readonly SqlMoney Zero = new SqlMoney (0);
56 public SqlMoney (decimal value)
58 if (value > 922337203685477.5807m || value < -922337203685477.5808m)
59 throw new OverflowException ();
60 this.value = Decimal.Round (value, 4);
64 public SqlMoney (double value) : this ((decimal)value)
68 public SqlMoney (int value) : this ((decimal)value)
72 public SqlMoney (long value) : this ((decimal)value)
81 get { return !notNull; }
84 public decimal Value {
87 throw new SqlNullValueException ();
97 public static SqlMoney Add (SqlMoney x, SqlMoney y)
102 public int CompareTo (object value)
106 else if (!(value is SqlMoney))
107 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
108 else if (((SqlMoney)value).IsNull)
111 return this.value.CompareTo (((SqlMoney)value).Value);
114 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
119 public override bool Equals (object value)
121 if (!(value is SqlMoney))
123 if (this.IsNull && ((SqlMoney)value).IsNull)
125 else if (((SqlMoney)value).IsNull)
128 return (bool) (this == (SqlMoney)value);
131 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
136 public override int GetHashCode ()
141 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
146 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
151 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
156 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
161 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
166 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
171 public static SqlMoney Parse (string s)
173 decimal d = Decimal.Parse (s);
175 if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value)
176 throw new OverflowException ("");
178 return new SqlMoney (d);
181 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
186 public decimal ToDecimal ()
191 public double ToDouble ()
193 return (double)value;
196 public int ToInt32 ()
198 return (int) Math.Round (value);
201 public long ToInt64 ()
203 return (long) Math.Round (value);
206 public SqlBoolean ToSqlBoolean ()
208 return ((SqlBoolean)this);
211 public SqlByte ToSqlByte ()
213 return ((SqlByte)this);
216 public SqlDecimal ToSqlDecimal ()
218 return ((SqlDecimal)this);
221 public SqlDouble ToSqlDouble ()
223 return ((SqlDouble)this);
226 public SqlInt16 ToSqlInt16 ()
228 return ((SqlInt16)this);
231 public SqlInt32 ToSqlInt32 ()
233 return ((SqlInt32)this);
236 public SqlInt64 ToSqlInt64 ()
238 return ((SqlInt64)this);
241 public SqlSingle ToSqlSingle ()
243 return ((SqlSingle)this);
246 public SqlString ToSqlString ()
248 return ((SqlString)this);
251 public override string ToString ()
256 return value.ToString ();
259 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
262 return new SqlMoney (x.Value + y.Value);
266 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
269 // FIXME: It's kinda mystery. should not be required.
270 decimal d = x.Value / y.Value;
271 return new SqlMoney (x.Value / y.Value);
275 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
277 if (x.IsNull || y.IsNull)
278 return SqlBoolean.Null;
280 return new SqlBoolean (x.Value == y.Value);
283 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
285 if (x.IsNull || y.IsNull)
286 return SqlBoolean.Null;
288 return new SqlBoolean (x.Value > y.Value);
291 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
293 if (x.IsNull || y.IsNull)
294 return SqlBoolean.Null;
296 return new SqlBoolean (x.Value >= y.Value);
299 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
301 if (x.IsNull || y.IsNull)
302 return SqlBoolean.Null;
304 return new SqlBoolean (!(x.Value == y.Value));
307 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
309 if (x.IsNull || y.IsNull)
310 return SqlBoolean.Null;
312 return new SqlBoolean (x.Value < y.Value);
315 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
317 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
318 return new SqlBoolean (x.Value <= y.Value);
321 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
324 return new SqlMoney (x.Value * y.Value);
328 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
331 return new SqlMoney (x.Value - y.Value);
335 public static SqlMoney operator - (SqlMoney n)
337 return new SqlMoney (-(n.Value));
340 public static explicit operator SqlMoney (SqlBoolean x)
346 return new SqlMoney ((decimal)x.ByteValue);
351 public static explicit operator SqlMoney (SqlDecimal x)
357 return new SqlMoney (x.Value);
362 public static explicit operator SqlMoney (SqlDouble x)
368 return new SqlMoney ((decimal)x.Value);
373 public static explicit operator decimal (SqlMoney x)
378 public static explicit operator SqlMoney (SqlSingle x)
384 return new SqlMoney ((decimal)x.Value);
389 public static explicit operator SqlMoney (SqlString x)
392 return SqlMoney.Parse (x.Value);
396 public static implicit operator SqlMoney (decimal x)
398 return new SqlMoney (x);
401 public static implicit operator SqlMoney (SqlByte x)
406 return new SqlMoney ((decimal)x.Value);
409 public static implicit operator SqlMoney (SqlInt16 x)
414 return new SqlMoney ((decimal)x.Value);
417 public static implicit operator SqlMoney (SqlInt32 x)
422 return new SqlMoney (x.Value);
425 public static implicit operator SqlMoney (SqlInt64 x)
430 return new SqlMoney (x.Value);