/// <summary> /// 15.7.4.7 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToPrecisionImpl(JsNumber target, JsInstance[] parameters) { if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) { return(ToStringImpl(target, new JsInstance[0])); } if (parameters.Length == 0) { throw new JsException(Global.SyntaxErrorClass.New("precision missing")); } if (parameters[0] == JsUndefined.Instance) { return(ToStringImpl(target, new JsInstance[0])); } int precision = 0; if (parameters.Length > 0) { precision = Convert.ToInt32(parameters[0].ToNumber()); } if (precision < 1 || precision > 21) { throw new JsException(Global.RangeErrorClass.New("precision must be between 1 and 21")); } // Get the number of decimals string str = target.ToNumber().ToString("e23", CultureInfo.InvariantCulture); int decimals = str.IndexOfAny(new char[] { '.', 'e' }); decimals = decimals == -1 ? str.Length : decimals; precision -= decimals; precision = precision < 1 ? 1 : precision; return(Global.StringClass.New(target.ToNumber().ToString("f" + precision, CultureInfo.InvariantCulture))); }
/// <summary> /// 15.7.4.6 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToExponentialImpl(JsNumber target, JsInstance[] parameters) { if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) { return(ToStringImpl(target, new JsInstance[0])); } int fractions = 16; if (parameters.Length > 0) { fractions = Convert.ToInt32(parameters[0].ToNumber()); } if (fractions > 20 || fractions < 0) { throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20")); } string format = String.Concat("#.", new String('0', fractions), "e+0"); return(Global.StringClass.New(target.ToNumber().ToString(format, CultureInfo.InvariantCulture))); }
/// <summary> /// 15.7.4.5 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToFixedImpl(JsNumber target, JsInstance[] parameters) { int fractions = 0; if (parameters.Length > 0) { fractions = Convert.ToInt32(parameters[0].ToNumber()); } if (fractions > 20 || fractions < 0) { throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20")); } if (target == Global.NaN) { return(Global.StringClass.New(target.ToString())); } return(Global.StringClass.New(target.ToNumber().ToString("f" + fractions, CultureInfo.InvariantCulture))); }
/// <summary> /// 15.7.4.7 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToPrecisionImpl(JsNumber target, JsInstance[] parameters) { if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) { return ToStringImpl(target, new JsInstance[0]); } if (parameters.Length == 0) { throw new JsException(Global.SyntaxErrorClass.New("precision missing")); } if (parameters[0] == JsUndefined.Instance) { return ToStringImpl(target, new JsInstance[0]); } int precision = 0; if (parameters.Length > 0) { precision = Convert.ToInt32(parameters[0].ToNumber()); } if (precision < 1 || precision > 21) { throw new JsException(Global.RangeErrorClass.New("precision must be between 1 and 21")); } // Get the number of decimals string str = target.ToNumber().ToString("e23", CultureInfo.InvariantCulture); int decimals = str.IndexOfAny(new char[] { '.', 'e' }); decimals = decimals == -1 ? str.Length : decimals; precision -= decimals; precision = precision < 1 ? 1 : precision; return Global.StringClass.New(target.ToNumber().ToString("f" + precision, CultureInfo.InvariantCulture)); }
/// <summary> /// 15.7.4.6 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToExponentialImpl(JsNumber target, JsInstance[] parameters) { if (double.IsInfinity(target.ToNumber()) || double.IsNaN(target.ToNumber())) { return ToStringImpl(target, new JsInstance[0]); } int fractions = 16; if (parameters.Length > 0) { fractions = Convert.ToInt32(parameters[0].ToNumber()); } if (fractions > 20 || fractions < 0) { throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20")); } string format = String.Concat("#.", new String('0', fractions), "e+0"); return Global.StringClass.New(target.ToNumber().ToString(format, CultureInfo.InvariantCulture)); }
/// <summary> /// 15.7.4.5 /// </summary> /// <param name="target"></param> /// <param name="parameters"></param> /// <returns></returns> public JsInstance ToFixedImpl(JsNumber target, JsInstance[] parameters) { int fractions = 0; if (parameters.Length > 0) { fractions = Convert.ToInt32(parameters[0].ToNumber()); } if (fractions > 20 || fractions < 0) { throw new JsException(Global.SyntaxErrorClass.New("Fraction Digits must be greater than 0 and lesser than 20")); } if (target == Global.NaN) { return Global.StringClass.New(target.ToString()); } return Global.StringClass.New(target.ToNumber().ToString("f" + fractions, CultureInfo.InvariantCulture)); }