Exemplo n.º 1
0
 private static void CreateExponentialRepresentation(
     Span <char> decimal_digits,
     int length,
     int exponent,
     StackStringBuilder writer)
 {
     Debug.Assert(length != 0);
     writer.Append(decimal_digits[0]);
     if (length != 1)
     {
         writer.Append('.');
         writer.Append(decimal_digits, 1, length - 1);
     }
     writer.Append(exponent_character_);
     if (exponent < 0)
     {
         writer.Append('-');
         exponent = -exponent;
     }
     else if (exponent == 0)
     {
         writer.Append('0');
         return;
     }
     else
     {
         writer.Append('+');
     }
     Debug.Assert(exponent < 1e4);
     if (exponent >= 100)
     {
         writer.Append((char)('0' + exponent / 100));
         exponent %= 100;
         writer.Append((char)('0' + exponent / 10));
         exponent %= 10;
         writer.Append((char)('0' + exponent));
     }
     else if (exponent >= 10)
     {
         writer.Append((char)('0' + exponent / 10));
         exponent %= 10;
         writer.Append((char)('0' + exponent));
     }
     else
     {
         writer.Append((char)('0' + exponent));
     }
 }
Exemplo n.º 2
0
 private static void CreateDecimalRepresentation(
     Span <char> decimal_digits,
     int length,
     int decimal_point,
     int digits_after_point,
     StackStringBuilder writer)
 {
     // Create a representation that is padded with zeros if needed.
     if (decimal_point <= 0)
     {
         // "0.00000decimal_rep".
         writer.Append('0');
         if (digits_after_point > 0)
         {
             writer.Append('.');
             writer.Append('0', -decimal_point);
             Debug.Assert(length <= digits_after_point - (-decimal_point));
             writer.Append(decimal_digits, length);
             int remaining_digits = digits_after_point - (-decimal_point) - length;
             writer.Append('0', remaining_digits);
         }
     }
     else if (decimal_point >= length)
     {
         // "decimal_rep0000.00000" or "decimal_rep.0000"
         writer.Append(decimal_digits, length);
         writer.Append('0', decimal_point - length);
         if (digits_after_point > 0)
         {
             writer.Append('.');
             writer.Append('0', digits_after_point);
         }
     }
     else
     {
         // "decima.l_rep000"
         Debug.Assert(digits_after_point > 0);
         writer.Append(decimal_digits, decimal_point);
         writer.Append('.');
         Debug.Assert(length - decimal_point <= digits_after_point);
         writer.Append(decimal_digits, decimal_point,
                       length - decimal_point);
         int remaining_digits = digits_after_point - (length - decimal_point);
         writer.Append('0', remaining_digits);
     }
 }
Exemplo n.º 3
0
        internal unsafe static string TryFormat(double value)
        {
            Span <char> resultBuf = stackalloc char[24];
            var         ssb       = new StackStringBuilder(resultBuf);

            if (value < 0.0)
            {
                ssb.Append('-');
                value = -value;
            }

            GrisuDouble grisuDouble = new GrisuDouble(value);

            if (grisuDouble.IsSpecial)
            {
                return(null);
            }

            Span <char> decimal_rep = stackalloc char[18];

            int decimal_point;
            int decimal_rep_length;

            if (!DoubleToShortestAscii(ref grisuDouble, decimal_rep, out decimal_rep_length, out decimal_point))
            {
                return(null);
            }

            int decimalRepLength = decimal_rep_length;

            if (decimal_point < 1)
            {
                decimalRepLength += -decimal_point + 1;
            }
            else if (decimal_point >= decimal_rep_length)
            {
                decimalRepLength += decimal_point - decimal_rep_length + 1;
            }

            int exponent          = decimal_point - 1;
            int absExponent       = Math.Abs(exponent);
            int exponentRepLength = decimal_rep_length + 3;

            if (exponent < 0)
            {
                ++exponentRepLength;
            }
            if (absExponent >= 10)
            {
                ++exponentRepLength;
                if (absExponent >= 100)
                {
                    ++exponentRepLength;
                }
            }

            if (decimalRepLength <= exponentRepLength)
            {
                CreateDecimalRepresentation(decimal_rep, decimal_rep_length,
                                            decimal_point,
                                            Math.Max(0, decimal_rep_length - decimal_point),
                                            ssb);
            }
            else
            {
                CreateExponentialRepresentation(decimal_rep, decimal_rep_length, exponent,
                                                ssb);
            }

            return(ssb.ToString());
        }