* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / DoubleType.cs
1 //
2 // DoubleType.cs
3  /*
4   * Copyright (c) 2002-2003 Mainsoft Corporation.
5   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
6   *
7   * Permission is hereby granted, free of charge, to any person obtaining a
8   * copy of this software and associated documentation files (the "Software"),
9   * to deal in the Software without restriction, including without limitation
10   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11   * and/or sell copies of the Software, and to permit persons to whom the
12   * Software is furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23   * DEALINGS IN THE SOFTWARE.
24   */
25 /**
26  * Class that converts objects to double value.
27  */
28 using System;
29 using System.Globalization;
30 using System.ComponentModel;
31
32 namespace Microsoft.VisualBasic.CompilerServices
33 {
34         [StandardModule, EditorBrowsableAttribute(EditorBrowsableState.Never)] 
35         sealed public class DoubleType {
36                 private DoubleType () {}
37
38                 /**
39                  * Converts given string to double
40                  * @param value string to convert
41                  * @return double double representation of given string
42                  */
43                 public static double FromString(string Value) {
44                         return FromString(Value, null);
45                 }
46     
47                 /**
48                  * The method try to convert given string to double in a following way:
49                  * 1. If input string is null return 0.
50                  * 2. If input string represents number: return value of this number, 
51                  * @exception InvalidCastException - in case if number translation failed 
52                  * @param str - The string that converted to double
53                  * @return doubleThe value that extracted from the input string.
54                  * @see Microsoft.VisualBasic.VBUtils#isNumber
55                  */
56                 public static double FromString(string Value, NumberFormatInfo numberFormat) {
57                         if (Value == null)
58                                 return 0.0;
59
60                         //try {
61                                 double[] lRes = new double[1];
62                         try {
63                                 if (VBUtils.isNumber(Value, lRes))
64                                         return lRes[0];
65                         } catch (FormatException e) {
66                                 throw new InvalidCastException(
67                                         Utils.GetResourceString("InvalidCast_FromStringTo", 
68                                         Value, "Double"));
69                         }
70                         //}
71                         //catch (Exception e) {
72                         //      throw new InvalidCastException(
73                         //              Utils.GetResourceString("InvalidCast_FromStringTo", 
74                         //              Value, "Double"), e);
75                         //}
76                         return 0.0;
77                 }
78
79                 /**
80                  * Converts given object to double.
81                  * @param value value to convert to
82                  * @return double value converted from given object
83                  */
84                 public static double FromObject(object Value) {
85                         return FromObject(Value, null);
86                 }
87     
88                 /**
89                  * The method converts given object to double by the following logic:
90                  * 1. If input object is null - return 0
91                  * 2. If input object is String - run FromString method
92                  * 3. Otherwise run .NET default conversion - Convert.ToDouble
93                  * @param value - The object that going to be converted
94                  * @return double The double value that converted from the source object
95                  * @see system.Convert#ToDouble
96                  */
97                 public static double FromObject(object Value, NumberFormatInfo numberFormat) {
98                         if (Value == null)
99                                 return 0.0;
100
101                         if (Value is string)
102                                 return FromString((string) Value, numberFormat);
103
104                         if (Value is bool)
105                                 return - (Convert.ToDouble (Value));
106
107                         //try {
108                         return Convert.ToDouble(Value, numberFormat);
109                         //}
110                         //catch(java.lang.Exception e) {
111                         //      throw new InvalidCastException(
112                         //              Utils.GetResourceString("InvalidCast_FromTo", 
113                         //              Utils.VBFriendlyName(Value), "Double"));
114                         //}
115                 }
116
117
118                 /**
119                  * Parse given string to double value
120                  * @param value string to parse
121                  * @return double resulted value 
122                  */
123                 public static double Parse(string Value) {
124                         return Parse(Value, null);
125                 }
126
127
128                 internal static bool TryParse(string Value, out double result) {
129                         return  Double.TryParse(Value, NumberStyles.Any, null, out result);
130                 }
131
132                 /**
133                  * This method try to parse given string using all available styles, if an 
134                  * error is thrown then it parses without a currency style.
135                  * @param value string to parse
136                  * @param numberFormat NumberFormatInfo to use
137                  * @return double the resulted value
138                  */
139                 public static double Parse(string Value, NumberFormatInfo numberFormat) {
140                         double d;
141
142                         try {
143                                 //                      d = ClrDouble.Parse(Value, NumberStyles.Any, numberFormat);
144                                 d = double.Parse(Value, NumberStyles.Any, numberFormat);
145                         }
146                         catch /*(Exception e)*/ {
147                                 //                      d = ClrDouble.Parse(Value, 255, numberFormat);
148                                 d = double.Parse(Value, (NumberStyles)255, numberFormat);
149                         }
150                         return d;
151                 }
152         }
153 }