2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Microsoft.VisualBasic / 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                         if (VBUtils.isNumber(Value, lRes))
63                                 return lRes[0];
64                         //}
65                         //catch (Exception e) {
66                         //      throw new InvalidCastException(
67                         //              Utils.GetResourceString("InvalidCast_FromStringTo", 
68                         //              Value, "Double"), e);
69                         //}
70                         return 0.0;
71                 }
72
73                 /**
74                  * Converts given object to double.
75                  * @param value value to convert to
76                  * @return double value converted from given object
77                  */
78                 public static double FromObject(object Value) {
79                         return FromObject(Value, null);
80                 }
81     
82                 /**
83                  * The method converts given object to double by the following logic:
84                  * 1. If input object is null - return 0
85                  * 2. If input object is String - run FromString method
86                  * 3. Otherwise run .NET default conversion - Convert.ToDouble
87                  * @param value - The object that going to be converted
88                  * @return double The double value that converted from the source object
89                  * @see system.Convert#ToDouble
90                  */
91                 public static double FromObject(object Value, NumberFormatInfo numberFormat) {
92                         if (Value == null)
93                                 return 0.0;
94
95                         if (Value is string)
96                                 return FromString((string) Value, numberFormat);
97
98                         //try {
99                         return Convert.ToDouble(Value, numberFormat);
100                         //}
101                         //catch(java.lang.Exception e) {
102                         //      throw new InvalidCastException(
103                         //              Utils.GetResourceString("InvalidCast_FromTo", 
104                         //              Utils.VBFriendlyName(Value), "Double"));
105                         //}
106                 }
107
108
109                 /**
110                  * Parse given string to double value
111                  * @param value string to parse
112                  * @return double resulted value 
113                  */
114                 public static double Parse(string Value) {
115                         return Parse(Value, null);
116                 }
117
118                 public static bool TryParse(string Value, out double result) {
119                         return  Double.TryParse(Value, NumberStyles.Any, null, out result);
120                 }
121
122                 /**
123                  * This method try to parse given string using all available styles, if an 
124                  * error is thrown then it parses without a currency style.
125                  * @param value string to parse
126                  * @param numberFormat NumberFormatInfo to use
127                  * @return double the resulted value
128                  */
129                 public static double Parse(string Value, NumberFormatInfo numberFormat) {
130                         double d;
131
132                         try {
133                                 //                      d = ClrDouble.Parse(Value, NumberStyles.Any, numberFormat);
134                                 d = double.Parse(Value, NumberStyles.Any, numberFormat);
135                         }
136                         catch /*(Exception e)*/ {
137                                 //                      d = ClrDouble.Parse(Value, 255, numberFormat);
138                                 d = double.Parse(Value, (NumberStyles)255, numberFormat);
139                         }
140                         return d;
141                 }
142         }
143 }