Example #1
0
 public static string toLocaleString(object thisObj)
 {
     if (!Convert.IsNumber(thisObj))
     {
         throw new JScriptException(JSError.NumberExpected);
     }
     else
     {
         return(Convert.ToNumber(thisObj).ToString("N"));
     }
 }
Example #2
0
 public static object valueOf(object thisObj)
 {
     if (!Convert.IsNumber(thisObj))
     {
         throw new JScriptException(JSError.NumberExpected);
     }
     else
     {
         return(Convert.ToNumber(thisObj));
     }
 }
Example #3
0
        public static string toFixed(object thisObj, double fractionDigits)
        {
            if (!Convert.IsNumber(thisObj))
            {
                throw new JScriptException(JSError.NumberExpected);
            }

            double value = Convert.ToNumber(thisObj);
            int    prec  = Convert.ToInt32(fractionDigits);

            if (prec < 0 || prec > 21)
            {
                throw new JScriptException(JSError.PrecisionOutOfRange);
            }

            return(value.ToString("F" + prec, CultureInfo.InvariantCulture));
        }
Example #4
0
        public static string toString(object thisObj, object radix)
        {
            if (!Convert.IsNumber(thisObj))
            {
                throw new JScriptException(JSError.NumberExpected);
            }

            double value = Convert.ToNumber(thisObj);

            if (Double.IsNaN(value))
            {
                return("NaN");
            }
            else if (Double.IsPositiveInfinity(value))
            {
                return("Infinity");
            }
            else if (Double.IsNegativeInfinity(value))
            {
                return("-Infinity");
            }

            int _radix = 10;

            if (radix != null)
            {
                _radix = Convert.ToInt32(radix);
                if (_radix < 2)
                {
                    _radix = 10;
                }
                else if (_radix > Digits.Length)
                {
                    _radix = 10;
                }
            }
            if (_radix == 10)
            {
                return(value.ToString(CultureInfo.InvariantCulture));
            }

            string result   = "";
            bool   negative = false;

            if (value < 0)
            {
                negative = true;
                value    = Math.Abs(value);
            }

            long   whole = (long)value;
            double frac  = value - whole;
            long   digit;

            while (whole >= 1)
            {
                whole  = Math.DivRem(whole, _radix, out digit);
                result = Digits [digit] + result;
            }

            if (result.Length == 0)
            {
                result = "0";
            }

            int    frac_digits = _radix;
            string frac_buf    = "";
            bool   has_frac    = false;

            while (frac != 0 && frac_digits < 50)
            {
                frac *= _radix;
                digit = (long)frac;
                frac -= digit;

                if (digit == 0)
                {
                    frac_buf += "0";
                }
                else
                {
                    if (!has_frac)
                    {
                        result += ".";
                    }
                    result  += frac_buf + Digits [digit];
                    frac_buf = "";
                    has_frac = true;
                }

                frac_digits++;
            }

            if (negative)
            {
                return("-" + result);
            }
            else
            {
                return(result);
            }
        }