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
57 public static readonly SqlMoney MaxValue = new SqlMoney (922337203685477.5807m);
58 public static readonly SqlMoney MinValue = new SqlMoney (-922337203685477.5808m);
59 public static readonly SqlMoney Null;
60 public static readonly SqlMoney Zero = new SqlMoney (0);
62 private static readonly NumberFormatInfo MoneyFormat;
70 MoneyFormat = (NumberFormatInfo) NumberFormatInfo.InvariantInfo.Clone ();
71 MoneyFormat.NumberDecimalDigits = 4;
72 MoneyFormat.NumberGroupSeparator = String.Empty;
75 public SqlMoney (decimal value)
77 if (value > 922337203685477.5807m || value < -922337203685477.5808m)
78 throw new OverflowException ();
79 this.value = Decimal.Round (value, 4);
83 public SqlMoney (double value) : this ((decimal)value)
87 public SqlMoney (int value) : this ((decimal)value)
91 public SqlMoney (long value) : this ((decimal)value)
100 get { return !notNull; }
103 public decimal Value {
106 throw new SqlNullValueException ();
116 public static SqlMoney Add (SqlMoney x, SqlMoney y)
121 public int CompareTo (object value)
125 else if (!(value is SqlMoney))
126 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlMoney"));
127 return CompareSqlMoney ((SqlMoney)value);
130 private int CompareSqlMoney (SqlMoney value)
135 return this.value.CompareTo (value.Value);
139 public int CompareTo (SqlMoney value)
141 return CompareSqlMoney (value);
145 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
150 public override bool Equals (object value)
152 if (!(value is SqlMoney))
154 if (this.IsNull && ((SqlMoney)value).IsNull)
156 else if (((SqlMoney)value).IsNull)
159 return (bool) (this == (SqlMoney)value);
162 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
167 public override int GetHashCode ()
172 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
177 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
182 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
187 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
192 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
197 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
202 public static SqlMoney Parse (string s)
204 decimal d = Decimal.Parse (s);
206 if (d > SqlMoney.MaxValue.Value || d < SqlMoney.MinValue.Value)
207 throw new OverflowException ("");
209 return new SqlMoney (d);
212 public static SqlMoney Subtract (SqlMoney x, SqlMoney y)
217 public decimal ToDecimal ()
222 public double ToDouble ()
224 return (double)value;
227 public int ToInt32 ()
229 return (int) Math.Round (value);
232 public long ToInt64 ()
234 return (long) Math.Round (value);
237 public SqlBoolean ToSqlBoolean ()
239 return ((SqlBoolean)this);
242 public SqlByte ToSqlByte ()
244 return ((SqlByte)this);
247 public SqlDecimal ToSqlDecimal ()
249 return ((SqlDecimal)this);
252 public SqlDouble ToSqlDouble ()
254 return ((SqlDouble)this);
257 public SqlInt16 ToSqlInt16 ()
259 return ((SqlInt16)this);
262 public SqlInt32 ToSqlInt32 ()
264 return ((SqlInt32)this);
267 public SqlInt64 ToSqlInt64 ()
269 return ((SqlInt64)this);
272 public SqlSingle ToSqlSingle ()
274 return ((SqlSingle)this);
277 public SqlString ToSqlString ()
279 return ((SqlString)this);
282 public override string ToString ()
287 return value.ToString ("N", MoneyFormat);
290 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
293 return new SqlMoney (x.Value + y.Value);
297 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
300 // FIXME: It's kinda mystery. should not be required.
301 decimal d = x.Value / y.Value;
302 return new SqlMoney (x.Value / y.Value);
306 public static SqlBoolean operator == (SqlMoney x, SqlMoney y)
308 if (x.IsNull || y.IsNull)
309 return SqlBoolean.Null;
311 return new SqlBoolean (x.Value == y.Value);
314 public static SqlBoolean operator > (SqlMoney x, SqlMoney y)
316 if (x.IsNull || y.IsNull)
317 return SqlBoolean.Null;
319 return new SqlBoolean (x.Value > y.Value);
322 public static SqlBoolean operator >= (SqlMoney x, SqlMoney y)
324 if (x.IsNull || y.IsNull)
325 return SqlBoolean.Null;
327 return new SqlBoolean (x.Value >= y.Value);
330 public static SqlBoolean operator != (SqlMoney x, SqlMoney y)
332 if (x.IsNull || y.IsNull)
333 return SqlBoolean.Null;
335 return new SqlBoolean (!(x.Value == y.Value));
338 public static SqlBoolean operator < (SqlMoney x, SqlMoney y)
340 if (x.IsNull || y.IsNull)
341 return SqlBoolean.Null;
343 return new SqlBoolean (x.Value < y.Value);
346 public static SqlBoolean operator <= (SqlMoney x, SqlMoney y)
348 if (x.IsNull || y.IsNull) return SqlBoolean.Null;
349 return new SqlBoolean (x.Value <= y.Value);
352 public static SqlMoney operator * (SqlMoney x, SqlMoney y)
355 return new SqlMoney (x.Value * y.Value);
359 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
362 return new SqlMoney (x.Value - y.Value);
366 public static SqlMoney operator - (SqlMoney n)
368 return new SqlMoney (-(n.Value));
371 public static explicit operator SqlMoney (SqlBoolean x)
377 return new SqlMoney ((decimal)x.ByteValue);
382 public static explicit operator SqlMoney (SqlDecimal x)
388 return new SqlMoney (x.Value);
393 public static explicit operator SqlMoney (SqlDouble x)
399 return new SqlMoney ((decimal)x.Value);
404 public static explicit operator decimal (SqlMoney x)
409 public static explicit operator SqlMoney (SqlSingle x)
415 return new SqlMoney ((decimal)x.Value);
420 public static explicit operator SqlMoney (SqlString x)
423 return SqlMoney.Parse (x.Value);
427 public static implicit operator SqlMoney (decimal x)
429 return new SqlMoney (x);
432 public static implicit operator SqlMoney (SqlByte x)
437 return new SqlMoney ((decimal)x.Value);
440 public static implicit operator SqlMoney (SqlInt16 x)
445 return new SqlMoney ((decimal)x.Value);
448 public static implicit operator SqlMoney (SqlInt32 x)
453 return new SqlMoney (x.Value);
456 public static implicit operator SqlMoney (SqlInt64 x)
461 return new SqlMoney (x.Value);
465 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
467 XmlQualifiedName qualifiedName = new XmlQualifiedName ("decimal", "http://www.w3.org/2001/XMLSchema");
468 return qualifiedName;
472 XmlSchema IXmlSerializable.GetSchema ()
474 throw new NotImplementedException ();
478 void IXmlSerializable.ReadXml (XmlReader reader)
480 throw new NotImplementedException ();
484 void IXmlSerializable.WriteXml (XmlWriter writer)
486 throw new NotImplementedException ();