5 // Cesar Lopez Nataren (cesar@ciencias.unam.mx)
7 // (C) 2003, Cesar Lopez Nataren
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
35 namespace Microsoft.JScript {
37 public class NumberPrototype : NumberObject {
39 internal static NumberPrototype Proto = new NumberPrototype ();
41 public static NumberConstructor constructor {
42 get { return NumberConstructor.Ctr; }
45 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_toExponential)]
46 public static string toExponential (object thisObj, object fractionDigits)
48 throw new NotImplementedException ();
51 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_toFixed)]
52 public static string toFixed (object thisObj, double fractionDigits)
54 if (!Convert.IsNumber (thisObj))
55 throw new JScriptException (JSError.NumberExpected);
57 double value = Convert.ToNumber (thisObj);
58 int prec = Convert.ToInt32 (fractionDigits);
60 if (prec < 0 || prec > 21)
61 throw new JScriptException (JSError.PrecisionOutOfRange);
63 return value.ToString ("F" + prec, CultureInfo.InvariantCulture);
66 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_toLocaleString)]
67 public static string toLocaleString (object thisObj)
69 if (!Convert.IsNumber (thisObj))
70 throw new JScriptException (JSError.NumberExpected);
72 return Convert.ToNumber (thisObj).ToString ("N");
75 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_toPrecision)]
76 public static string toPrecision (object thisObj, object precision)
78 throw new NotImplementedException ();
81 internal static char [] Digits = new char [] {
82 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
83 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
84 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
85 'u', 'v', 'w', 'x', 'y', 'z'
90 // We aren't 100% compatible to MS JS.NET here, because we sometimes produce slightly more
91 // of the fractional digits. This shouldn't cause any trouble.
93 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_toString)]
94 public static string toString (object thisObj, object radix)
96 if (!Convert.IsNumber (thisObj))
97 throw new JScriptException (JSError.NumberExpected);
99 double value = Convert.ToNumber (thisObj);
101 if (Double.IsNaN (value))
103 else if (Double.IsPositiveInfinity (value))
105 else if (Double.IsNegativeInfinity (value))
110 _radix = Convert.ToInt32 (radix);
113 else if (_radix > Digits.Length)
117 return value.ToString (CultureInfo.InvariantCulture);
120 bool negative = false;
123 value = Math.Abs (value);
126 long whole = (long) value;
127 double frac = value - whole;
131 whole = Math.DivRem (whole, _radix, out digit);
132 result = Digits [digit] + result;
135 if (result.Length == 0)
138 int frac_digits = _radix;
139 string frac_buf = "";
140 bool has_frac = false;
142 while (frac != 0 && frac_digits < 50) {
150 if (!has_frac) result += ".";
151 result += frac_buf + Digits [digit];
165 [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.Number_valueOf)]
166 public static object valueOf (object thisObj)
168 if (!Convert.IsNumber (thisObj))
169 throw new JScriptException (JSError.NumberExpected);
171 return Convert.ToNumber (thisObj);