示例#1
0
        /// <summary>
        /// Convert a 64-bit floating point number to a fixed notation string.
        /// </summary>
        internal static void ToFixedString(StringBuilder result, ulong ieeeMantissa,
                                           uint ieeeExponent, int precision, RyuFormatOptions options,
                                           NumberFormatInfo info)
        {
            bool zero = true, soft = (options & RyuFormatOptions.SoftPrecision) != 0;
            int  exponent = Decode(ieeeMantissa, ieeeExponent, out ulong mantissa), start =
                result.Length;
            ulong mShift = mantissa << MANTISSA_SHIFT;
            uint  digits;

            if (exponent >= -RyuFloat64.DOUBLE_MANTISSA_BITS)
            {
                int idx = (exponent < 0) ? 0 : RyuUtils.IndexForExponent(exponent), p10bits =
                    Pow10BitsForIndex(idx), i = RyuUtils.LengthForIndex(idx) - 1, p =
                    RyuTables.POW10_OFFSET_D[idx] + i, j = p10bits - exponent + MANTISSA_SHIFT;
                for (; i >= 0; i--)
                {
                    digits = RyuUtils.MulShiftMod1E9(mShift, RyuTables.POW10_SPLIT_D[p, 0],
                                                     RyuTables.POW10_SPLIT_D[p, 1], RyuTables.POW10_SPLIT_D[p, 2], j);
                    if (!zero)
                    {
                        RyuUtils.Append9Digits(result, digits);
                    }
                    else if (digits != 0U)
                    {
                        RyuUtils.AppendNDigits(result, RyuUtils.DecimalLength9(digits),
                                               digits);
                        zero = false;
                    }
                    p--;
                }
            }

            if (zero)
            {
                result.Append(RyuUtils.ZERO);
            }
            if ((options & RyuFormatOptions.ThousandsSeparators) != 0)
            {
                RyuUtils.AddThousands(result, start, info);
            }
            if (precision > 0 && (!soft || exponent < 0))
            {
                result.Append(info.NumberDecimalSeparator);
            }

            if (exponent < 0)
            {
                // 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd
                int idx = (-exponent) >> 4, roundFlag = 0, i = 0, blocks = precision / 9 + 1,
                    minBlock = RyuTables.MIN_BLOCK_2_D[idx];
                if (blocks <= minBlock)
                {
                    RyuUtils.Append0(result, precision);
                    i = blocks;
                }
                else if (i < minBlock)
                {
                    RyuUtils.Append0(result, 9 * minBlock);
                    i = minBlock;
                }
                int p = RyuTables.POW10_OFFSET_2_D[idx] + i - minBlock, pMax = RyuTables.
                                                                               POW10_OFFSET_2_D[idx + 1], j = MANTISSA_SHIFT + POW10_ADDITIONAL_BITS -
                                                                                                              exponent - (idx << 4);
                for (; i < blocks; ++i)
                {
                    if (p >= pMax)
                    {
                        // If the remaining digits are all 0, then no rounding required
                        if (!soft)
                        {
                            RyuUtils.Append0(result, precision - 9 * i);
                        }
                        break;
                    }
                    digits = RyuUtils.MulShiftMod1E9(mShift, RyuTables.POW10_SPLIT_2_D[p, 0],
                                                     RyuTables.POW10_SPLIT_2_D[p, 1], RyuTables.POW10_SPLIT_2_D[p, 2], j);
                    if (i < blocks - 1)
                    {
                        RyuUtils.Append9Digits(result, digits);
                    }
                    else
                    {
                        int  maximum   = precision - 9 * i;
                        uint lastDigit = RyuUtils.LastDigit(ref digits, 9 - maximum);
                        // Is m * 10^(additionalDigits + 1) / 2^(-e2) integer?
                        if (lastDigit > 5U)
                        {
                            roundFlag = 1;
                        }
                        else if (lastDigit < 5U)
                        {
                            roundFlag = 0;
                        }
                        else if (HasTrailingZeroes(exponent, precision + 1, mantissa))
                        {
                            roundFlag = 2;
                        }
                        else
                        {
                            roundFlag = 1;
                        }
                        if (maximum > 0)
                        {
                            RyuUtils.AppendCDigits(result, digits, maximum);
                        }
                        break;
                    }
                    p++;
                }
                if (roundFlag != 0 && RyuUtils.RoundResult(result, start, roundFlag,
                                                           out int decimalIndex, info))
                {
                    if (decimalIndex > 0)
                    {
                        result[decimalIndex++] = RyuUtils.ZERO;
                        result[decimalIndex]   = info.NumberDecimalSeparator[0];
                    }
                    result.Append(RyuUtils.ZERO);
                }
                if (soft && precision > 0)
                {
                    RyuUtils.SoftenResult(result, info);
                }
            }
            else if (!soft)
            {
                RyuUtils.Append0(result, precision);
            }
        }