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];
63 if (VBUtils.isNumber(Value, lRes))
65 } catch (FormatException e) {
66 throw new InvalidCastException(
67 Utils.GetResourceString("InvalidCast_FromStringTo",
71 //catch (Exception e) {
72 // throw new InvalidCastException(
73 // Utils.GetResourceString("InvalidCast_FromStringTo",
74 // Value, "Double"), e);
80 * Converts given object to double.
81 * @param value value to convert to
82 * @return double value converted from given object
84 public static double FromObject(object Value) {
85 return FromObject(Value, null);
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
97 public static double FromObject(object Value, NumberFormatInfo numberFormat) {
102 return FromString((string) Value, numberFormat);
105 return - (Convert.ToDouble (Value));
108 return Convert.ToDouble(Value, numberFormat);
110 //catch(java.lang.Exception e) {
111 // throw new InvalidCastException(
112 // Utils.GetResourceString("InvalidCast_FromTo",
113 // Utils.VBFriendlyName(Value), "Double"));
119 * Parse given string to double value
120 * @param value string to parse
121 * @return double resulted value
123 public static double Parse(string Value) {
124 return Parse(Value, null);
128 internal static bool TryParse(string Value, out double result) {
129 return Double.TryParse(Value, NumberStyles.Any, null, out result);
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
139 public static double Parse(string Value, NumberFormatInfo numberFormat) {
143 // d = ClrDouble.Parse(Value, NumberStyles.Any, numberFormat);
144 d = double.Parse(Value, NumberStyles.Any, numberFormat);
146 catch /*(Exception e)*/ {
147 // d = ClrDouble.Parse(Value, 255, numberFormat);
148 d = double.Parse(Value, (NumberStyles)255, numberFormat);