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 return Convert
.ToSingle(Value
,numberFormat
);
68 //Actually we may need to downcast to long if H or O
70 //This gets base correct, but this conversion does not allow base, so I
71 // think the java base check is unneeded
74 //if(Value.Substring(0,1) == "&"){
76 // if(Value.Substring(1,1).ToUpper() == "H")
78 // else if(Value.Substring(1,1).ToUpper() == "B")
81 // // I think we should just let convert take care of the execption.
82 // // Should we throw a special execption instead?
83 // // I think the Mainsoft java code below just converts execptions from java to C#
88 //return Convert.ToSingle(Value.Substring(start,Value.Length - start), Base);
90 // This is the java code for the above line. I think .net throws the correct execpition.
91 // verify correct implmentation and execptions and remove
95 // double[] lRes = new double[1];
96 // if (VBUtils.isNumber(Value, lRes))
97 // return (float)lRes[0];
99 //catch (java.lang.Exception e)
101 // throw new InvalidCastException(
102 // Utils.GetResourceString("InvalidCast_FromStringTo",
103 // value, "Single"), e);
109 * Converts given object to float.
110 * @param float value to convert to
111 * @return float value converted from given object
113 public static float FromObject(object Value
) {
114 return FromObject(Value
, null);
118 * The method converts given object to float by the following logic:
119 * 1. If input object is null - return 0
120 * 2. If input object is String - run FromString method
121 * 3. Otherwise run .NET default conversion - Convert.ToSingle
122 * @param value - The object that going to be converted
123 * @return float The float value that converted from the source object
124 * @see system.Convert#ToSingle
126 public static float FromObject(object Value
, NumberFormatInfo numberFormat
) {
131 return FromString((string) Value
, numberFormat
);
133 return Convert
.ToSingle(Value
, numberFormat
);
134 // This is the java code for the above line. I think .net throws the correct execpition.
135 //verify correct execptions and remove
138 // return Convert.ToSingle(Value, numberFormat);
140 // catch(java.lang.Exception e)
142 // throw new InvalidCastException(
143 // Utils.GetResourceString("InvalidCast_FromTo",
144 // Utils.VBFriendlyName(value), "Single"));