5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Dennis Hayes (dennish@raytek.com)
8 // (C) copyright 2002 Chris J Breisch
11 * Copyright (c) 2002-2003 Mainsoft Corporation.
12 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
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.
33 * Class that converts objects to single value.
36 using System.ComponentModel;
37 using System.Globalization;
39 namespace Microsoft.VisualBasic.CompilerServices {
40 [StandardModuleAttribute, EditorBrowsableAttribute(EditorBrowsableState.Never)]
41 sealed public class SingleType {
42 private SingleType () {}
45 * Converts given string to float
46 * @param value string to convert
47 * @return float float representation of given string
49 public static float FromString(string Value) {
50 return FromString(Value, null);
54 * The method try to convert given string to float in a following way:
55 * 1. If input string is null return 0.
56 * 2. If input string represents number: return value of this number,
57 * @exception InvalidCastException - in case if number translation failed
58 * @param str - The string that converted to float
59 * @return float The value that extracted from the input string.
60 * @see Microsoft.VisualBasic.VBUtils#isNumber
62 public static float FromString(string Value, NumberFormatInfo numberFormat) {
66 long [] val = new long [1];
68 if (StringType.IsHexOrOctValue (Value, val))
69 return (float) val [0];
71 return Convert.ToSingle(Value,numberFormat);
72 } catch (FormatException e) {
73 throw new InvalidCastException(
74 Utils.GetResourceString("InvalidCast_FromStringTo",
79 //Actually we may need to downcast to long if H or O
81 //This gets base correct, but this conversion does not allow base, so I
82 // think the java base check is unneeded
85 //if(Value.Substring(0,1) == "&"){
87 // if(Value.Substring(1,1).ToUpper() == "H")
89 // else if(Value.Substring(1,1).ToUpper() == "B")
92 // // I think we should just let convert take care of the execption.
93 // // Should we throw a special execption instead?
94 // // I think the Mainsoft java code below just converts execptions from java to C#
99 //return Convert.ToSingle(Value.Substring(start,Value.Length - start), Base);
101 // This is the java code for the above line. I think .net throws the correct execpition.
102 // verify correct implmentation and execptions and remove
106 // double[] lRes = new double[1];
107 // if (VBUtils.isNumber(Value, lRes))
108 // return (float)lRes[0];
110 //catch (java.lang.Exception e)
112 // throw new InvalidCastException(
113 // Utils.GetResourceString("InvalidCast_FromStringTo",
114 // value, "Single"), e);
120 * Converts given object to float.
121 * @param float value to convert to
122 * @return float value converted from given object
124 public static float FromObject(object Value) {
125 return FromObject(Value, null);
129 * The method converts given object to float by the following logic:
130 * 1. If input object is null - return 0
131 * 2. If input object is String - run FromString method
132 * 3. Otherwise run .NET default conversion - Convert.ToSingle
133 * @param value - The object that going to be converted
134 * @return float The float value that converted from the source object
135 * @see system.Convert#ToSingle
137 public static float FromObject(object Value, NumberFormatInfo numberFormat) {
142 return FromString((string) Value, numberFormat);
144 return Convert.ToSingle(Value, numberFormat);
145 // This is the java code for the above line. I think .net throws the correct execpition.
146 //verify correct execptions and remove
149 // return Convert.ToSingle(Value, numberFormat);
151 // catch(java.lang.Exception e)
153 // throw new InvalidCastException(
154 // Utils.GetResourceString("InvalidCast_FromTo",
155 // Utils.VBFriendlyName(value), "Single"));