* Interaction.cs :
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / SingleType.cs
1 //
2 // SingleType.cs
3 //
4 //      Author:
5 //      Chris J Breisch (cjbreisch@altavista.net) 
6 //      Dennis Hayes (dennish@raytek.com)
7 //
8 //      (C) copyright 2002 Chris J Breisch
9 //
10 /*
11   * Copyright (c) 2002-2003 Mainsoft Corporation.
12   * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13   *
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:
20   * 
21   * The above copyright notice and this permission notice shall be included in
22   * all copies or substantial portions of the Software.
23   * 
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.
31   */
32   /**
33    * Class that converts objects to single value.
34    */
35 using System;
36 using System.ComponentModel;
37 using System.Globalization;
38
39 namespace Microsoft.VisualBasic.CompilerServices {
40         [StandardModuleAttribute, EditorBrowsableAttribute(EditorBrowsableState.Never)] 
41         sealed public class SingleType {
42                 private SingleType () {}
43
44                 /**
45                  * Converts given string to float
46                  * @param value string to convert
47                  * @return float float representation of given string
48                  */
49                 public static float FromString(string Value) {
50                         return FromString(Value, null);
51                 }
52     
53                 /**
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
61                  */
62                 public static float FromString(string Value, NumberFormatInfo numberFormat) {
63                         if (Value == null)
64                                 return 0.0f;
65
66                         long [] val = new long [1];
67                         try {
68                                 if (StringType.IsHexOrOctValue (Value, val))
69                                         return (float) val [0];
70
71                                 return Convert.ToSingle(Value,numberFormat);
72                         } catch (FormatException e) {
73                                 throw new InvalidCastException(
74                                         Utils.GetResourceString("InvalidCast_FromStringTo", 
75                                         Value, "Float"));
76                         }
77                         return 0.0f;
78
79                         //Actually we may need to downcast to long if H or O
80
81                         //This gets base correct, but this conversion does not allow base, so I 
82                         // think the java base check is unneeded
83                         //int Base = 10;
84                         //int start = 0;
85                         //if(Value.Substring(0,1) == "&"){
86                         //      //is diff base
87                         //      if(Value.Substring(1,1).ToUpper() == "H")
88                         //              Base = 16;
89                         //      else if(Value.Substring(1,1).ToUpper() == "B")
90                         //              Base = 8;
91                         //      else {
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#
95                         //      }
96                         //      start = 2;
97                         //}
98
99                         //return Convert.ToSingle(Value.Substring(start,Value.Length - start), Base);
100
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
103                         //
104                         //try
105                         //{
106                         //    double[] lRes = new double[1];
107                         //    if (VBUtils.isNumber(Value, lRes))
108                         //        return (float)lRes[0];
109                         //}
110                         //catch (java.lang.Exception e)
111                         //{
112                         //    throw new InvalidCastException(
113                         //        Utils.GetResourceString("InvalidCast_FromStringTo", 
114                         //           value, "Single"), e);
115                         //}
116                         //return 0.0f;
117                 }
118     
119                 /**
120                  * Converts given object to float.
121                  * @param float value to convert to
122                  * @return float value converted from given object
123                  */
124                 public static float FromObject(object Value) {
125                         return FromObject(Value, null);
126                 }
127
128                 /**
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
136                  */
137                 public static float FromObject(object Value, NumberFormatInfo numberFormat) {
138                         if (Value == null)
139                                 return 0.0f;
140
141                         if (Value is string)
142                                 return FromString((string) Value, numberFormat);
143
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
147                         // try
148                         // {
149                         //     return Convert.ToSingle(Value, numberFormat);
150                         // }
151                         // catch(java.lang.Exception e)
152                         // {
153                         //     throw new InvalidCastException(
154                         //         Utils.GetResourceString("InvalidCast_FromTo", 
155                         //             Utils.VBFriendlyName(value), "Single"));
156                         // }
157                 }
158
159         }
160 }