Exemplo n.º 1
0
        /// <summary>Converts a <c>decimal</c> under the Common Language
        /// Infrastructure (see
        /// <see cref='PeterO.Numbers.EDecimal'>"Forms of numbers"</see> ) to
        /// an arbitrary-precision decimal.</summary>
        /// <param name='dec'>A <c>decimal</c> under the Common Language
        /// Infrastructure (usually a.NET Framework decimal).</param>
        /// <returns>An arbitrary-precision decimal floating-point
        /// number.</returns>
        public static EDecimal FromDecimal(decimal dec)
        {
            return

                (EDecimal.FromString(
                     dec.ToString(System.Globalization.CultureInfo.InvariantCulture)));
        }
Exemplo n.º 2
0
        internal static EFloat FromString(
            string chars,
            int offset,
            int length,
            EContext ctx,
            bool throwException)
        {
            if (chars == null)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new ArgumentNullException(nameof(chars));
                }
            }
            if (offset < 0)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new FormatException("offset(" + offset + ") is not" +
                                              "\u0020greater" + "\u0020or equal to 0");
                }
            }
            if (offset > chars.Length)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new FormatException("offset(" + offset + ") is not" +
                                              "\u0020less" + "\u0020or" + "\u0020equal to " + chars.Length);
                }
            }
            if (length < 0)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new FormatException("length(" + length + ") is not" +
                                              "\u0020greater or" + "\u0020equal to 0");
                }
            }
            if (length > chars.Length)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new FormatException("length(" + length + ") is not" +
                                              "\u0020less" + "\u0020or" + "\u0020equal to " + chars.Length);
                }
            }
            if (chars.Length - offset < length)
            {
                if (!throwException)
                {
                    return(null);
                }
                else
                {
                    throw new FormatException("str's length minus " + offset + "(" +
                                              (chars.Length - offset) + ") is not greater or equal to " + length);
                }
            }
            EContext b64 = EContext.Binary64;

            if (ctx != null && ctx.HasMaxPrecision && ctx.HasExponentRange &&
                !ctx.IsSimplified && ctx.EMax.CompareTo(b64.EMax) <= 0 &&
                ctx.EMin.CompareTo(b64.EMin) >= 0 &&
                ctx.Precision.CompareTo(b64.Precision) <= 0)
            {
                int tmpoffset = offset;
                int endpos    = offset + length;
                if (length == 0)
                {
                    if (!throwException)
                    {
                        return(null);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                if (chars[tmpoffset] == '-' || chars[tmpoffset] == '+')
                {
                    ++tmpoffset;
                }
                if (tmpoffset < endpos && ((chars[tmpoffset] >= '0' &&
                                            chars[tmpoffset] <= '9') || chars[tmpoffset] == '.'))
                {
                    EFloat ef = DoubleEFloatFromString(
                        chars,
                        offset,
                        length,
                        ctx,
                        throwException);
                    if (ef != null)
                    {
                        return(ef);
                    }
                }
            }
            return(EDecimal.FromString(
                       chars,
                       offset,
                       length,
                       EContext.Unlimited.WithSimplified(ctx != null && ctx.IsSimplified))
                   .ToEFloat(ctx));
        }