public static void Optimized_N_Digits(StringBuilder b, double v, int decimals)
        {
            long power10 = Power10.Power10Signed64[decimals];

            if (v < 0)
            {
                v = -v; b.Append('-');
            }                                   // make v >= 0
            var  fv = Math.Floor(v);
            long i  = (long)fv;
            long f  = (long)Math.Floor((v - fv) * power10 + 0.5);

            OptimizedLongFormatter.HeaplessAppend(b, i);
            if (decimals > 0 && f != 0)
            {
                b.Append('.');
                for (int d = decimals - OptimizedLongFormatter.GetDigitsCount(f); d > 0; d--)
                {
                    b.Append('0');
                }

                OptimizedLongFormatter.HeaplessAppend(b, f);
            }
            else
            {
                b.Append(".0");
            }
        }
Exemplo n.º 2
0
        public unsafe static void Optimized_N_Digits(StringBuilder b, double v, int decimals)
        {
            long power10 = Power10.Power10Signed64[decimals];

            if (v < 0)
            {
                v = -v; b.Append('-');
            }
            var  fv = Math.Floor(v);
            long i  = (long)fv;
            long f  = (long)Math.Floor((v - fv) * power10 + 0.5d);

            OptimizedLongFormatter.HeaplessAppend(b, i);
            if (decimals > 0 && f != 0)
            {
                b.Append('.');
                char *chars = stackalloc char[decimals];
                for (int pos = decimals - 1; pos >= 0; pos--)
                {
                    chars[pos] = (char)(f % 10 + 48);
                    f          = f / 10;
                }

                if (false)
                {
                    for (int pos = 0; pos < decimals; pos++)
                    {
                        b.Append(chars[pos]);
                    }
                }
                else
                {
                    int lastDecimal = decimals - 1;
                    while (lastDecimal > 0 && chars[lastDecimal] == '0')
                    {
                        lastDecimal--;
                    }
                    for (int pos = 0; pos <= lastDecimal; pos++)
                    {
                        b.Append(chars[pos]);
                    }
                }
            }
            else
            {
                b.Append(".0");
            }
        }
Exemplo n.º 3
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
     if (value == null)
     {
         writer.WriteNull();
     }
     else
     {
         StringBuilder stringBuilder;
         if (value is long[] arr)
         {
             int len = arr.Length;
             stringBuilder = new StringBuilder(len << 1);
             // for (int pos = 0; pos < len; pos++)
             for (int pos = 0; pos < arr.Length; pos++)
             {
                 if (pos != 0)
                 {
                     stringBuilder.Append(',');
                 }
                 OptimizedLongFormatter.HeaplessAppend(stringBuilder, arr[pos]);
             }
         }
         else if (value is long?[] arrNullable)