Пример #1
0
        internal static string ToString(double d)
        {
            if (d > long.MinValue && d < long.MaxValue && Math.Abs(d % 1) <= DoubleIsIntegerTolerance)
            {
                // we are dealing with integer that can be cached
                return(ToString((long)d));
            }

            return(NumberPrototype.ToNumberString(d));
        }
Пример #2
0
        internal static string ToString(double d)
        {
            if (d > long.MinValue && d < long.MaxValue && Math.Abs(d % 1) <= DoubleIsIntegerTolerance)
            {
                // we are dealing with integer that can be cached
                return(ToString((long)d));
            }

            using var stringBuilder = StringBuilderPool.Rent();
            // we can create smaller array as we know the format to be short
            return(NumberPrototype.NumberToString(d, new DtoaBuilder(17), stringBuilder.Builder));
        }
Пример #3
0
        public static string ToString(EcmaValue value, int radix)
        {
            ThrowIfArgumentNotBigInt(value);
            if (radix < 2 || radix > 36)
            {
                throw new EcmaRangeErrorException(InternalString.Error.InvalidToStringRadix);
            }
            if (EcmaValue.GetNumberCoercion(value) == EcmaNumberType.BigInt64)
            {
                return(NumberPrototype.GetString(value.ToInt64(), radix));
            }
            BigInteger bigInt = ToBigInteger(value);

            if (radix == 10)
            {
                return(bigInt.ToString());
            }
            if (radix == 16)
            {
                return(bigInt.ToString("x"));
            }
            int  digits  = biggestLong[radix - 2];
            bool isMinus = bigInt < 0;

            if (isMinus)
            {
                bigInt = -bigInt;
            }
            BigInteger    divisor = BigInteger.Pow(radix, digits);
            StringBuilder sb      = new StringBuilder();

            while (bigInt > divisor)
            {
                bigInt = BigInteger.DivRem(bigInt, divisor, out BigInteger remainder);
                string s = NumberPrototype.GetString((long)remainder, radix);
                sb.Insert(0, s);
                if (s.Length < digits)
                {
                    sb.Insert(0, "0", digits - s.Length);
                }
            }
            sb.Insert(0, NumberPrototype.GetString((long)bigInt, radix));
            if (isMinus)
            {
                sb.Insert(0, '-');
            }
            return(sb.ToString());
        }
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToString(JsValue o)
        {
            if (o == null)
            {
                return(null);
            }
            if (o.IsObject())
            {
                var p = o.AsObject() as IPrimitiveInstance;
                if (p != null)
                {
                    o = p.PrimitiveValue;
                }
            }

            if (o.IsString())
            {
                return(o.AsString());
            }

            if (o == Undefined.Instance)
            {
                return(Undefined.Text);
            }

            if (o == Null.Instance)
            {
                return(Null.Text);
            }

            if (o.IsBoolean())
            {
                return(o.AsBoolean() ? "true" : "false");
            }

            if (o.IsNumber())
            {
                return(NumberPrototype.ToNumberString(o.AsNumber()));
            }

            return(ToString(ToPrimitive(o, Types.String)));
        }
Пример #5
0
        public void ShouldConvertNumbersToDifferentBase(string expected, long number, int radix)
        {
            var result = NumberPrototype.ToBase(number, radix);

            Assert.Equal(expected, result);
        }
Пример #6
0
 public void ShouldComputeFractionInBase()
 {
     Assert.Equal("011", NumberPrototype.ToFractionBase(0.375, 2));
     Assert.Equal("14141414141414141414141414141414141414141414141414", NumberPrototype.ToFractionBase(0.375, 5));
 }