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);
52 private static readonly NumberFormatInfo MoneyFormat;
60 MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
61 MoneyFormat.NumberDecimalDigits = 4;
62 MoneyFormat.NumberGroupSeparator = String.Empty;
65 public SqlMoney (decimal value)
67 if (value > 922337203685477.5807m || value < -922337203685477.5808m)
68 throw new OverflowException ();
69 this.value = Decimal.Round (value, 4);
73 public SqlMoney (double value) : this ((decimal)value)
77 public SqlMoney (int value) : this ((decimal)value)
81 public SqlMoney (long value) : this ((decimal)value)
90 get { return !notNull; }
93 public decimal Value {
96 throw new SqlNullValueException ();
106 public static SqlMoney Add (SqlMoney x, SqlMoney y)
111 public int CompareTo (object value)
115 else if (!(value is SqlMoney))
116 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
117 return CompareSqlMoney ((SqlMoney)value);
120 private int CompareSqlMoney (SqlMoney value)
125 return this.value.CompareTo (value.Value);
129 public int CompareTo (SqlMoney value)
131 return CompareSqlMoney (value);
135 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
140 public override bool Equals (object value)
142 if (!(value is SqlMoney))
144 if (this.IsNull && ((SqlMoney)value).IsNull)
146 else if (((SqlMoney)value).IsNull)
149 return (bool) (this == (SqlMoney)value);
152 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
157 public override int GetHashCode ()
162 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
167 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
172 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
177 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
182 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
187 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
192 public static SqlMoney Parse (string s)
194 decimal d = Decimal.Parse (s);
196 if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value)
197 throw new OverflowException ("");
199 return new SqlMoney (d);
202 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
207 public decimal ToDecimal ()
212 public double ToDouble ()
214 return (double)value;
217 public int ToInt32 ()
219 return (int) Math.Round (value);
222 public long ToInt64 ()
224 return (long) Math.Round (value);
227 public SqlBoolean ToSqlBoolean ()
229 return ((SqlBoolean)this);
232 public SqlByte ToSqlByte ()
234 return ((SqlByte)this);
237 public SqlDecimal ToSqlDecimal ()
239 return ((SqlDecimal)this);
242 public SqlDouble ToSqlDouble ()
244 return ((SqlDouble)this);
247 public SqlInt16 ToSqlInt16 ()
249 return ((SqlInt16)this);
252 public SqlInt32 ToSqlInt32 ()
254 return ((SqlInt32)this);
257 public SqlInt64 ToSqlInt64 ()
259 return ((SqlInt64)this);
262 public SqlSingle ToSqlSingle ()
264 return ((SqlSingle)this);
267 public SqlString ToSqlString ()
269 return ((SqlString)this);
272 public override string ToString ()
277 return value.ToString ("N", MoneyFormat);
280 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
283 return new SqlMoney (x.Value + y.Value);
287 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
290 // FIXME: It's kinda mystery. should not be required.
291 decimal d = x.Value / y.Value;
292 return new SqlMoney (x.Value / y.Value);
296 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
298 if (x.IsNull || y.IsNull)
299 return SqlBoolean.Null;
301 return new SqlBoolean (x.Value == y.Value);
304 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
306 if (x.IsNull || y.IsNull)
307 return SqlBoolean.Null;
309 return new SqlBoolean (x.Value > y.Value);
312 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
314 if (x.IsNull || y.IsNull)
315 return SqlBoolean.Null;
317 return new SqlBoolean (x.Value >= y.Value);
320 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
322 if (x.IsNull || y.IsNull)
323 return SqlBoolean.Null;
325 return new SqlBoolean (!(x.Value == y.Value));
328 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
330 if (x.IsNull || y.IsNull)
331 return SqlBoolean.Null;
333 return new SqlBoolean (x.Value < y.Value);
336 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
338 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
339 return new SqlBoolean (x.Value <= y.Value);
342 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
345 return new SqlMoney (x.Value * y.Value);
349 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
352 return new SqlMoney (x.Value - y.Value);
356 public static SqlMoney operator - (SqlMoney n)
358 return new SqlMoney (-(n.Value));
361 public static explicit operator SqlMoney (SqlBoolean x)
367 return new SqlMoney ((decimal)x.ByteValue);
372 public static explicit operator SqlMoney (SqlDecimal x)
378 return new SqlMoney (x.Value);
383 public static explicit operator SqlMoney (SqlDouble x)
389 return new SqlMoney ((decimal)x.Value);
394 public static explicit operator decimal (SqlMoney x)
399 public static explicit operator SqlMoney (SqlSingle x)
405 return new SqlMoney ((decimal)x.Value);
410 public static explicit operator SqlMoney (SqlString x)
413 return SqlMoney.Parse (x.Value);
417 public static implicit operator SqlMoney (decimal x)
419 return new SqlMoney (x);
422 public static implicit operator SqlMoney (SqlByte x)
427 return new SqlMoney ((decimal)x.Value);
430 public static implicit operator SqlMoney (SqlInt16 x)
435 return new SqlMoney ((decimal)x.Value);
438 public static implicit operator SqlMoney (SqlInt32 x)
443 return new SqlMoney (x.Value);
446 public static implicit operator SqlMoney (SqlInt64 x)
451 return new SqlMoney (x.Value);