**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / DecimalType.cs
blob0d133255597ec9cdb7271e2335e758a9b690521f
1 //
2 // DecimalType.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 //
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.
32 using System;
33 using System.Globalization;
34 using System.ComponentModel;
36 namespace Microsoft.VisualBasic.CompilerServices {
37 [StandardModule, EditorBrowsable(EditorBrowsableState.Never)]
38 sealed public class DecimalType {
39 private DecimalType () {}
41 // Methods
42 /**
43 * This method converts given boolean to Decimal. true is converted to -1
44 * and false to 0.
45 * @param value The boolean that going to be converted
46 * @return Decimal The Decimal value that converted from the boolean
48 public static System.Decimal FromBoolean (System.Boolean Value) {
49 if (Value)return Decimal.MinusOne;
50 return Decimal.Zero;
53 public static System.Decimal FromString (System.String Value) {
54 return FromString(Value, null);
57 public static System.Decimal FromObject (System.Object Value) {
58 return DecimalType.FromObject(Value, null);
60 /**
61 * The method try to convert given string to Decimal in a following way:
62 * 1. If input string is null return 0.
63 * 2. If input string represents number: return value of this number,
64 * @exception OverflowException - if number is out of Decimal range
65 * @exception InvalidCastException - in case if number translation failed
66 * due to NumberFormatException.
67 * @exception All other thrown exceptions from Decimal.Parse
68 * @param str - The string that converted to Decimal
69 * @return Decimal The value that extracted from the input string.
70 * @see Microsoft.VisualBasic.VBUtils#isNumber
72 public static Decimal FromString(String Value, NumberFormatInfo numberFormat) {
73 if (Value == null)return Decimal.Zero;
75 //TODO: remove this line
76 //return Parse(Value, numberFormat);
78 //TODO convert this to C# and uncomment
79 //try {
80 //double d;
81 long[] lRes = new long[1];
82 bool b = StringType.IsHexOrOctValue(Value, lRes);
83 if (b == true)return (decimal)lRes[0];
84 return Parse(Value, numberFormat);
85 //}
86 //catch (OverflowException exp) {
87 // throw (RuntimeException)ExceptionUtils.VbMakeException(6);
88 //}
89 //catch (FormatException exp) {
90 // throw new InvalidCastException(
91 // Utils.GetResourceString("InvalidCast_FromStringTo",
92 // str, "Decimal"));
93 //}
95 /**
96 * The method converts given object to decimal by the following logic:
97 * 1. If input object is null - return 0
98 * 2. If input object is String - run FromString method
99 * 3. Otherwise run .NET default conversion - Convert.ToDecimal
100 * @param value - The object that going to be converted
101 * @return Decimal The Decimal value that converted from the source object
102 * @see system.Convert#ToDecimal
104 public static System.Decimal FromObject (System.Object Value, System.Globalization.NumberFormatInfo NumberFormat) {
105 if (Value == null)return Decimal.Zero;
107 if (Value is string)return FromString((string) Value, NumberFormat);
109 return Convert.ToDecimal(Value);
113 * This method try to parse this string first of all by allowing that the
114 * string will contain currency symbol. if an error is thrown the a parse
115 * without a currenct is tried.
116 * @param value the string that should be parse to Decimal
117 * @param numberFormat the relevant NumberFormat.
118 * @return Decimal the Decimal value of the string.
120 public static System.Decimal Parse (System.String Value, System.Globalization.NumberFormatInfo NumberFormat) {
121 return Decimal.Parse(Value, NumberStyles.Any, NumberFormat);