* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / DecimalType.cs
1 //
2 // DecimalType.cs
3 //
4 //      Author:
5 //      Chris J Breisch (cjbreisch@altavista.net) 
6 //      Dennis Hayes (dennish@raytek.com)
7 //
8 //      (C) copyright 2002 Chris J Breisch
9 //
10  /*
11   * Copyright (c) 2002-2003 Mainsoft Corporation.
12   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13   *
14   * Permission is hereby granted, free of charge, to any person obtaining a
15   * copy of this software and associated documentation files (the "Software"),
16   * to deal in the Software without restriction, including without limitation
17   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18   * and/or sell copies of the Software, and to permit persons to whom the
19   * Software is furnished to do so, subject to the following conditions:
20   * 
21   * The above copyright notice and this permission notice shall be included in
22   * all copies or substantial portions of the Software.
23   * 
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30   * DEALINGS IN THE SOFTWARE.
31   */
32 using System;
33 using System.Globalization;
34 using System.ComponentModel;
35
36 namespace Microsoft.VisualBasic.CompilerServices {
37         [StandardModule, EditorBrowsable(EditorBrowsableState.Never)] 
38         sealed public class DecimalType {
39                 private DecimalType () {}
40
41                 // Methods
42                 /**
43                  * This method converts given boolean to Decimal. true is converted to -1
44                  * and false to 0. 
45                  * @param value The boolean that going to be converted
46                  * @return Decimal The Decimal value that converted from the boolean
47                  */
48                 public static System.Decimal FromBoolean (System.Boolean Value) {
49                         if (Value)return Decimal.MinusOne;
50                         return Decimal.Zero;
51                 }
52
53                 public static System.Decimal FromString (System.String Value) {
54                         return FromString(Value, null);
55                 }
56
57                 public static System.Decimal FromObject (System.Object Value) {
58                         return DecimalType.FromObject(Value, null);
59                 }
60                 /**
61                  * The method try to convert given string to Decimal in a following way:
62                  * 1. If input string is null return 0.
63                  * 2. If input string represents number: return value of this number, 
64                  * @exception OverflowException - if number is out of Decimal range
65                  * @exception InvalidCastException - in case if number translation failed 
66                  *  due to NumberFormatException.
67                  * @exception All other thrown exceptions from Decimal.Parse 
68                  * @param str - The string that converted to Decimal
69                  * @return Decimal The value that extracted from the input string.
70                  * @see Microsoft.VisualBasic.VBUtils#isNumber
71                  */
72                 public static Decimal FromString(String Value, NumberFormatInfo numberFormat) {
73                         if (Value == null)return Decimal.Zero;
74                         
75                         //TODO: remove this line
76                         //return Parse(Value, numberFormat);
77
78                         //TODO convert this to C# and uncomment
79                         try {
80                                 long[] lRes = new long[1];
81                                 bool b = StringType.IsHexOrOctValue(Value, lRes);
82                                 if (b == true)return (decimal)lRes[0];
83                                 return Parse(Value, numberFormat);
84                         }
85                         catch (FormatException exp) {
86                                 throw new InvalidCastException(
87                                         Utils.GetResourceString("InvalidCast_FromStringTo", 
88                                         Value, "Decimal"));
89                         }
90                 }
91                 /**
92                  * The method converts given object to decimal by the following logic:
93                  * 1. If input object is null - return 0
94                  * 2. If input object is String - run FromString method
95                  * 3. Otherwise run .NET default conversion - Convert.ToDecimal
96                  * @param value - The object that going to be converted
97                  * @return Decimal The Decimal value that converted from the source object
98                  * @see system.Convert#ToDecimal
99                  */
100                 public static System.Decimal FromObject (System.Object Value, System.Globalization.NumberFormatInfo NumberFormat) {
101                         if (Value == null)return Decimal.Zero;
102
103                         if (Value is string)return FromString((string) Value, NumberFormat);
104                         
105                         return Convert.ToDecimal(Value);
106                 }
107
108                 /**
109                  * This method try to parse this string first of all by allowing that the 
110                  * string will contain currency symbol. if an error is thrown the a parse
111                  * without a currenct is tried.
112                  * @param value the string that should be parse to Decimal
113                  * @param numberFormat the relevant NumberFormat.  
114                  * @return Decimal the Decimal value of the string.
115                  */
116                 public static System.Decimal Parse (System.String Value, System.Globalization.NumberFormatInfo NumberFormat) {
117                         return Decimal.Parse(Value, NumberStyles.Any, NumberFormat);
118                 }
119         }
120 }
121