(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlMoney.cs
blob1d9d10119850ee35c6b66d8282225705c68abcf5
1 //
2 // System.Data.SqlTypes.SqlMoney
3 //
4 // Author:
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
34 using System;
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlMoney : INullable, IComparable
41 #region Fields
43 decimal value;
45 private bool notNull;
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 #endregion
54 #region Constructors
56 public SqlMoney (decimal value)
58 if (value > 922337203685477.5807m || value < -922337203685477.5808m)
59 throw new OverflowException ();
60 this.value = Decimal.Round (value, 4);
61 notNull = true;
64 public SqlMoney (double value) : this ((decimal)value)
68 public SqlMoney (int value) : this ((decimal)value)
72 public SqlMoney (long value) : this ((decimal)value)
76 #endregion
78 #region Properties
80 public bool IsNull {
81 get { return !notNull; }
84 public decimal Value {
85 get {
86 if (this.IsNull)
87 throw new SqlNullValueException ();
88 else
89 return value;
93 #endregion
95 #region Methods
97 public static SqlMoney Add (SqlMoney x, SqlMoney y)
99 return (x + y);
102 public int CompareTo (object value)
104 if (value == null)
105 return 1;
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)
113 if (value.IsNull)
114 return 1;
115 else
116 return this.value.CompareTo (value.Value);
119 #if NET_2_0
120 public int CompareTo (SqlMoney value)
122 return CompareSqlMoney (value);
124 #endif
126 public static SqlMoney Divide (SqlMoney x, SqlMoney y)
128 return (x / y);
131 public override bool Equals (object value)
133 if (!(value is SqlMoney))
134 return false;
135 if (this.IsNull && ((SqlMoney)value).IsNull)
136 return true;
137 else if (((SqlMoney)value).IsNull)
138 return false;
139 else
140 return (bool) (this == (SqlMoney)value);
143 public static SqlBoolean Equals (SqlMoney x, SqlMoney y)
145 return (x == y);
148 public override int GetHashCode ()
150 return (int)value;
153 public static SqlBoolean GreaterThan (SqlMoney x, SqlMoney y)
155 return (x > y);
158 public static SqlBoolean GreaterThanOrEqual (SqlMoney x, SqlMoney y)
160 return (x >= y);
163 public static SqlBoolean LessThan (SqlMoney x, SqlMoney y)
165 return (x < y);
168 public static SqlBoolean LessThanOrEqual (SqlMoney x, SqlMoney y)
170 return (x <= y);
173 public static SqlMoney Multiply (SqlMoney x, SqlMoney y)
175 return (x * y);
178 public static SqlBoolean NotEquals (SqlMoney x, SqlMoney y)
180 return (x != 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)
195 return (x - y);
198 public decimal ToDecimal ()
200 return value;
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 ()
265 if (!notNull)
266 return "Null";
267 else
268 return value.ToString ();
271 public static SqlMoney operator + (SqlMoney x, SqlMoney y)
273 checked {
274 return new SqlMoney (x.Value + y.Value);
278 public static SqlMoney operator / (SqlMoney x, SqlMoney y)
280 checked {
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;
291 else
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;
299 else
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;
307 else
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;
315 else
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;
323 else
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)
335 checked {
336 return new SqlMoney (x.Value * y.Value);
340 public static SqlMoney operator - (SqlMoney x, SqlMoney y)
342 checked {
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)
354 if (x.IsNull)
355 return Null;
356 else {
357 checked {
358 return new SqlMoney ((decimal)x.ByteValue);
363 public static explicit operator SqlMoney (SqlDecimal x)
365 if (x.IsNull)
366 return Null;
367 else {
368 checked {
369 return new SqlMoney (x.Value);
374 public static explicit operator SqlMoney (SqlDouble x)
376 if (x.IsNull)
377 return Null;
378 else {
379 checked {
380 return new SqlMoney ((decimal)x.Value);
385 public static explicit operator decimal (SqlMoney x)
387 return x.Value;
390 public static explicit operator SqlMoney (SqlSingle x)
392 if (x.IsNull)
393 return Null;
394 else {
395 checked {
396 return new SqlMoney ((decimal)x.Value);
401 public static explicit operator SqlMoney (SqlString x)
403 checked {
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)
415 if (x.IsNull)
416 return Null;
417 else
418 return new SqlMoney ((decimal)x.Value);
421 public static implicit operator SqlMoney (SqlInt16 x)
423 if (x.IsNull)
424 return Null;
425 else
426 return new SqlMoney ((decimal)x.Value);
429 public static implicit operator SqlMoney (SqlInt32 x)
431 if (x.IsNull)
432 return Null;
433 else
434 return new SqlMoney (x.Value);
437 public static implicit operator SqlMoney (SqlInt64 x)
439 if (x.IsNull)
440 return Null;
441 else
442 return new SqlMoney (x.Value);
445 #endregion