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.
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
40 namespace System.Data.SqlTypes
43 [SerializableAttribute]
44 [XmlSchemaProvider ("GetXsdType")]
46 public struct SqlMoney : INullable, IComparable
53 private decimal value;
56 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685477.5807m);
57 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808m);
58 public static readonly SqlMoney Null;
59 public static readonly SqlMoney Zero = new SqlMoney (0);
61 private static readonly NumberFormatInfo MoneyFormat;
69 MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
70 MoneyFormat.NumberDecimalDigits = 4;
71 MoneyFormat.NumberGroupSeparator = String.Empty;
74 public SqlMoney (decimal value)
76 if (value > 922337203685477.5807m || value < -922337203685477.5808m)
77 throw new OverflowException ();
78 this.value = Decimal.Round (value, 4);
82 public SqlMoney (double value) : this ((decimal) value)
86 public SqlMoney (int value) : this ((decimal) value)
90 public SqlMoney (long value) : this ((decimal) value)
99 get { return !notNull; }
102 public decimal Value {
105 throw new SqlNullValueException ();
115 public static SqlMoney Add (SqlMoney x, SqlMoney y)
120 public int CompareTo (object value)
124 else if (!(value is SqlMoney))
125 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
126 return CompareSqlMoney ((SqlMoney)value);
129 private int CompareSqlMoney (SqlMoney value)
134 return this.value.CompareTo (value.Value);
138 public int CompareTo (SqlMoney value)
140 return CompareSqlMoney (value);
144 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
149 public override bool Equals (object value)
151 if (!(value is SqlMoney))
154 return ((SqlMoney)value).IsNull;
155 else if (((SqlMoney)value).IsNull)
158 return (bool) (this == (SqlMoney)value);
161 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
166 public override int GetHashCode ()
171 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
176 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
181 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
186 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
191 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
196 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
201 public static SqlMoney Parse (string s)
203 decimal d = Decimal.Parse (s);
205 if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value)
206 throw new OverflowException ();
208 return new SqlMoney (d);
211 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
216 public decimal ToDecimal ()
221 public double ToDouble ()
223 return (double) value;
226 public int ToInt32 ()
228 return (int) Math.Round (value);
231 public long ToInt64 ()
233 return (long) Math.Round (value);
236 public SqlBoolean ToSqlBoolean ()
238 return ((SqlBoolean) this);
241 public SqlByte ToSqlByte ()
243 return ((SqlByte) this);
246 public SqlDecimal ToSqlDecimal ()
248 return ((SqlDecimal) this);
251 public SqlDouble ToSqlDouble ()
253 return ((SqlDouble) this);
256 public SqlInt16 ToSqlInt16 ()
258 return ((SqlInt16) this);
261 public SqlInt32 ToSqlInt32 ()
263 return ((SqlInt32) this);
266 public SqlInt64 ToSqlInt64 ()
268 return ((SqlInt64) this);
271 public SqlSingle ToSqlSingle ()
273 return ((SqlSingle) this);
276 public SqlString ToSqlString ()
278 return ((SqlString) this);
281 public override string ToString ()
286 return value.ToString ("N", MoneyFormat);
289 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
292 return new SqlMoney (x.Value + y.Value);
296 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
299 return new SqlMoney (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)
330 return SqlBoolean.Null;
332 return new SqlBoolean (!(x.Value == y.Value));
335 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
337 if (x.IsNull || y.IsNull)
338 return SqlBoolean.Null;
340 return new SqlBoolean (x.Value < y.Value);
343 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
345 if (x.IsNull || y.IsNull)
346 return SqlBoolean.Null;
347 return new SqlBoolean (x.Value <= y.Value);
350 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
353 return new SqlMoney (x.Value * y.Value);
357 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
360 return new SqlMoney (x.Value - y.Value);
364 public static SqlMoney operator - (SqlMoney x)
366 return new SqlMoney (-(x.Value));
369 public static explicit operator SqlMoney (SqlBoolean x)
375 return new SqlMoney ((decimal) x.ByteValue);
380 public static explicit operator SqlMoney (SqlDecimal x)
386 return new SqlMoney (x.Value);
391 public static explicit operator SqlMoney (SqlDouble x)
397 return new SqlMoney ((decimal) x.Value);
402 public static explicit operator decimal (SqlMoney x)
407 public static explicit operator SqlMoney (SqlSingle x)
413 return new SqlMoney ((decimal) x.Value);
418 public static explicit operator SqlMoney (SqlString x)
421 return SqlMoney.Parse (x.Value);
426 public static explicit operator SqlMoney (double x)
428 return new SqlMoney (x);
431 public static implicit operator SqlMoney (long x)
433 return new SqlMoney (x);
437 public static implicit operator SqlMoney (decimal x)
439 return new SqlMoney (x);
442 public static implicit operator SqlMoney (SqlByte x)
447 return new SqlMoney ((decimal) x.Value);
450 public static implicit operator SqlMoney (SqlInt16 x)
455 return new SqlMoney ((decimal) x.Value);
458 public static implicit operator SqlMoney (SqlInt32 x)
463 return new SqlMoney (x.Value);
466 public static implicit operator SqlMoney (SqlInt64 x)
471 return new SqlMoney (x.Value);
475 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
477 XmlQualifiedName qualifiedName = new XmlQualifiedName ("decimal", "http://www.w3.org/2001/XMLSchema");
478 return qualifiedName;
482 XmlSchema IXmlSerializable.GetSchema ()
484 throw new NotImplementedException ();
488 void IXmlSerializable.ReadXml (XmlReader reader)
490 throw new NotImplementedException ();
494 void IXmlSerializable.WriteXml (XmlWriter writer)
496 throw new NotImplementedException ();