2005-05-13 Atsushi Enomoto <atsushi@ximian.com>
[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                         return Convert.ToSingle(Value,numberFormat);
67
68                         //Actually we may need to downcast to long if H or O
69
70                         //This gets base correct, but this conversion does not allow base, so I 
71                         // think the java base check is unneeded
72                         //int Base = 10;
73                         //int start = 0;
74                         //if(Value.Substring(0,1) == "&"){
75                         //      //is diff base
76                         //      if(Value.Substring(1,1).ToUpper() == "H")
77                         //              Base = 16;
78                         //      else if(Value.Substring(1,1).ToUpper() == "B")
79                         //              Base = 8;
80                         //      else {
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#
84                         //      }
85                         //      start = 2;
86                         //}
87
88                         //return Convert.ToSingle(Value.Substring(start,Value.Length - start), Base);
89
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
92                         //
93                         //try
94                         //{
95                         //    double[] lRes = new double[1];
96                         //    if (VBUtils.isNumber(Value, lRes))
97                         //        return (float)lRes[0];
98                         //}
99                         //catch (java.lang.Exception e)
100                         //{
101                         //    throw new InvalidCastException(
102                         //        Utils.GetResourceString("InvalidCast_FromStringTo", 
103                         //           value, "Single"), e);
104                         //}
105                         //return 0.0f;
106                 }
107     
108                 /**
109                  * Converts given object to float.
110                  * @param float value to convert to
111                  * @return float value converted from given object
112                  */
113                 public static float FromObject(object Value) {
114                         return FromObject(Value, null);
115                 }
116
117                 /**
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
125                  */
126                 public static float FromObject(object Value, NumberFormatInfo numberFormat) {
127                         if (Value == null)
128                                 return 0.0f;
129
130                         if (Value is string)
131                                 return FromString((string) Value, numberFormat);
132
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
136                         // try
137                         // {
138                         //     return Convert.ToSingle(Value, numberFormat);
139                         // }
140                         // catch(java.lang.Exception e)
141                         // {
142                         //     throw new InvalidCastException(
143                         //         Utils.GetResourceString("InvalidCast_FromTo", 
144                         //             Utils.VBFriendlyName(value), "Single"));
145                         // }
146                 }
147
148         }
149 }