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 return CompareSqlMoney ((SqlMoney)value);
111 private int CompareSqlMoney (SqlMoney value)
116 return this.value.CompareTo (value.Value);
120 public int CompareTo (SqlMoney value)
122 return CompareSqlMoney (value);
126 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
131 public override bool Equals (object value)
133 if (!(value is SqlMoney))
135 if (this.IsNull && ((SqlMoney)value).IsNull)
137 else if (((SqlMoney)value).IsNull)
140 return (bool) (this == (SqlMoney)value);
143 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
148 public override int GetHashCode ()
153 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
158 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
163 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
168 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
173 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
178 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
183 public static SqlMoney Parse (string s)
185 decimal d = Decimal.Parse (s);
187 if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value)
188 throw new OverflowException ("");
190 return new SqlMoney (d);
193 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
198 public decimal ToDecimal ()
203 public double ToDouble ()
205 return (double)value;
208 public int ToInt32 ()
210 return (int) Math.Round (value);
213 public long ToInt64 ()
215 return (long) Math.Round (value);
218 public SqlBoolean ToSqlBoolean ()
220 return ((SqlBoolean)this);
223 public SqlByte ToSqlByte ()
225 return ((SqlByte)this);
228 public SqlDecimal ToSqlDecimal ()
230 return ((SqlDecimal)this);
233 public SqlDouble ToSqlDouble ()
235 return ((SqlDouble)this);
238 public SqlInt16 ToSqlInt16 ()
240 return ((SqlInt16)this);
243 public SqlInt32 ToSqlInt32 ()
245 return ((SqlInt32)this);
248 public SqlInt64 ToSqlInt64 ()
250 return ((SqlInt64)this);
253 public SqlSingle ToSqlSingle ()
255 return ((SqlSingle)this);
258 public SqlString ToSqlString ()
260 return ((SqlString)this);
263 public override string ToString ()
268 return value.ToString ();
271 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
274 return new SqlMoney (x.Value + y.Value);
278 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
281 // FIXME: It's kinda mystery. should not be required.
282 decimal d = x.Value / y.Value;
283 return new SqlMoney (x.Value / y.Value);
287 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
289 if (x.IsNull || y.IsNull)
290 return SqlBoolean.Null;
292 return new SqlBoolean (x.Value == y.Value);
295 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
297 if (x.IsNull || y.IsNull)
298 return SqlBoolean.Null;
300 return new SqlBoolean (x.Value > y.Value);
303 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
305 if (x.IsNull || y.IsNull)
306 return SqlBoolean.Null;
308 return new SqlBoolean (x.Value >= y.Value);
311 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
313 if (x.IsNull || y.IsNull)
314 return SqlBoolean.Null;
316 return new SqlBoolean (!(x.Value == y.Value));
319 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
321 if (x.IsNull || y.IsNull)
322 return SqlBoolean.Null;
324 return new SqlBoolean (x.Value < y.Value);
327 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
329 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
330 return new SqlBoolean (x.Value <= y.Value);
333 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
336 return new SqlMoney (x.Value * y.Value);
340 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
343 return new SqlMoney (x.Value - y.Value);
347 public static SqlMoney operator - (SqlMoney n)
349 return new SqlMoney (-(n.Value));
352 public static explicit operator SqlMoney (SqlBoolean x)
358 return new SqlMoney ((decimal)x.ByteValue);
363 public static explicit operator SqlMoney (SqlDecimal x)
369 return new SqlMoney (x.Value);
374 public static explicit operator SqlMoney (SqlDouble x)
380 return new SqlMoney ((decimal)x.Value);
385 public static explicit operator decimal (SqlMoney x)
390 public static explicit operator SqlMoney (SqlSingle x)
396 return new SqlMoney ((decimal)x.Value);
401 public static explicit operator SqlMoney (SqlString x)
404 return SqlMoney.Parse (x.Value);
408 public static implicit operator SqlMoney (decimal x)
410 return new SqlMoney (x);
413 public static implicit operator SqlMoney (SqlByte x)
418 return new SqlMoney ((decimal)x.Value);
421 public static implicit operator SqlMoney (SqlInt16 x)
426 return new SqlMoney ((decimal)x.Value);
429 public static implicit operator SqlMoney (SqlInt32 x)
434 return new SqlMoney (x.Value);
437 public static implicit operator SqlMoney (SqlInt64 x)
442 return new SqlMoney (x.Value);