public static String toExponential(Object thisob, Object fractionDigits) { double value = NumberPrototype.ThisobToDouble(thisob); double f; if (fractionDigits == null || fractionDigits is Missing) { f = 16; } else { f = Convert.ToInteger(fractionDigits); } if (f < 0 || f > 20) { throw new JScriptException(JSError.FractionOutOfRange); } StringBuilder fmt = new StringBuilder("#."); for (int i = 0; i < f; i++) { fmt.Append('0'); } fmt.Append("e+0"); return(value.ToString(fmt.ToString(), CultureInfo.InvariantCulture)); }
public static String toPrecision(Object thisob, Object precision) { double value = NumberPrototype.ThisobToDouble(thisob); if (precision == null || precision is Missing) { return(Convert.ToString(value)); } double df = Convert.ToInteger(precision); if (df < 1 || df > 21) { throw new JScriptException(JSError.PrecisionOutOfRange); } int f = (int)df; if (Double.IsNaN(value)) { return("NaN"); } if (Double.IsInfinity(value)) { return(value > 0.0 ? "Infinity" : "-Infinity"); } String sign; if (value >= 0.0) { sign = ""; } else { sign = "-"; value = -value; } String mant = value.ToString("e" + (f - 1).ToString(), CultureInfo.InvariantCulture); int exp = Int32.Parse(mant.Substring(mant.Length - 4), CultureInfo.InvariantCulture); mant = mant.Substring(0, 1) + mant.Substring(2, f - 1); if (exp >= f || exp < -6) { return(sign + mant.Substring(0, 1) + (f > 1 ? "." + mant.Substring(1) : "") + (exp >= 0 ? "e+" : "e") + exp.ToString()); } if (exp == f - 1) { return(sign + mant); } if (exp >= 0) { return(sign + mant.Substring(0, exp + 1) + "." + mant.Substring(exp + 1)); } return(sign + "0." + mant.PadLeft(f - exp - 1, '0')); }
public static String toFixed(Object thisob, double fractionDigits) { double value = NumberPrototype.ThisobToDouble(thisob); if (Double.IsNaN(fractionDigits)) { fractionDigits = 0; } else if (fractionDigits < 0 || fractionDigits > 20) { throw new JScriptException(JSError.FractionOutOfRange); } return(value.ToString("f" + ((int)fractionDigits).ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)); }