4 * Copyright (c) 2002-2003 Mainsoft Corporation.
5 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 * Class that converts objects to double value.
29 using System.Globalization;
30 using System.ComponentModel;
32 namespace Microsoft.VisualBasic.CompilerServices
34 [StandardModule, EditorBrowsableAttribute(EditorBrowsableState.Never)]
35 sealed public class DoubleType {
36 private DoubleType () {}
39 * Converts given string to double
40 * @param value string to convert
41 * @return double double representation of given string
43 public static double FromString(string Value) {
44 return FromString(Value, null);
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
56 public static double FromString(string Value, NumberFormatInfo numberFormat) {
61 double[] lRes = new double[1];
62 if (VBUtils.isNumber(Value, lRes))
65 //catch (Exception e) {
66 // throw new InvalidCastException(
67 // Utils.GetResourceString("InvalidCast_FromStringTo",
68 // Value, "Double"), e);
74 * Converts given object to double.
75 * @param value value to convert to
76 * @return double value converted from given object
78 public static double FromObject(object Value) {
79 return FromObject(Value, null);
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
91 public static double FromObject(object Value, NumberFormatInfo numberFormat) {
96 return FromString((string) Value, numberFormat);
99 return Convert.ToDouble(Value, numberFormat);
101 //catch(java.lang.Exception e) {
102 // throw new InvalidCastException(
103 // Utils.GetResourceString("InvalidCast_FromTo",
104 // Utils.VBFriendlyName(Value), "Double"));
110 * Parse given string to double value
111 * @param value string to parse
112 * @return double resulted value
114 public static double Parse(string Value) {
115 return Parse(Value, null);
118 public static bool TryParse(string Value, out double result) {
119 return Double.TryParse(Value, NumberStyles.Any, null, out result);
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
129 public static double Parse(string Value, NumberFormatInfo numberFormat) {
133 // d = ClrDouble.Parse(Value, NumberStyles.Any, numberFormat);
134 d = double.Parse(Value, NumberStyles.Any, numberFormat);
136 catch /*(Exception e)*/ {
137 // d = ClrDouble.Parse(Value, 255, numberFormat);
138 d = double.Parse(Value, (NumberStyles)255, numberFormat);