Example #1
0
 public static StringBuilder GetFormatedDateTimeForFilename(this StringBuilder sb, DateTime value)
 {
     sb.Clear();
     //sb.ConcatFormat("{0}{1: yyyy-MM-dd HH:mm:ss.fff}{2}", before, value, after);
     sb.Concat(value.Year, 0, '0', 10, false);
     sb.Concat(value.Month, 2);
     sb.Concat(value.Day, 2);
     sb.Append("_");
     sb.Concat(value.Hour, 2);
     sb.Concat(value.Minute, 2);
     sb.Concat(value.Second, 2);
     return(sb);
 }
Example #2
0
 public static StringBuilder GetFormatedDateTimeOffset(this StringBuilder sb, string before, DateTime value, string after = "")
 {
     sb.Clear();
     //sb.ConcatFormat("{0}{1: yyyy-MM-dd HH:mm:ss.fff}{2}", before, value, after);
     sb.Append(before);
     sb.Concat(value.Year, 4, '0', 10, false);
     sb.Append("-");
     sb.Concat(value.Month, 2);
     sb.Append("-");
     sb.Concat(value.Day, 2);
     sb.Append(" ");
     sb.Concat(value.Hour, 2);
     sb.Append(":");
     sb.Concat(value.Minute, 2);
     sb.Append(":");
     sb.Concat(value.Second, 2);
     sb.Append(".");
     sb.Concat(value.Millisecond, 3);
     sb.Append(after);
     return(sb);
 }
 //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Assume base ten.
 public static StringBuilder Concat(this StringBuilder string_builder, uint uint_val, uint pad_amount, char pad_char)
 {
     string_builder.Concat(uint_val, pad_amount, pad_char, 10, true);
     return(string_builder);
 }
 //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Assume no padding and base ten.
 public static StringBuilder Concat(this StringBuilder string_builder, uint uint_val)
 {
     string_builder.Concat(uint_val, 0, ms_default_pad_char, 10, true);
     return(string_builder);
 }
 //! Convert a given float value to a string and concatenate onto the stringbuilder.
 public static StringBuilder Concat(this StringBuilder string_builder, double double_val, uint decimal_places, uint pad_amount)
 {
     string_builder.Concat(double_val, decimal_places, pad_amount, ms_default_pad_char, false);
     return(string_builder);
 }
 //! Convert a given float value to a string and concatenate onto the stringbuilder. Assumes five decimal places, and no padding.
 public static StringBuilder Concat(this StringBuilder string_builder, double double_val)
 {
     string_builder.Concat(double_val, ms_default_decimal_places, 0, ms_default_pad_char, false);
     return(string_builder);
 }
        //! Convert a given float value to a string and concatenate onto the stringbuilder
        public static StringBuilder Concat(this StringBuilder string_builder, double double_val, uint decimal_places, uint pad_amount, char pad_char, bool thousandSeparator)
        {
            Debug.Assert(pad_amount >= 0);

            if (decimal_places == 0)
            {
                // No decimal places, just round up and print it as an int

                // Agh, Math.Floor() just works on doubles/decimals. Don't want to cast! Let's do this the old-fashioned way.
                long int_val;
                if (double_val >= 0.0)
                {
                    // Round up
                    int_val = (long)(double_val + 0.5);
                }
                else
                {
                    // Round down for negative numbers
                    int_val = (long)(double_val - 0.5);
                }
                string_builder.Concat(int_val, pad_amount, pad_char, 10, thousandSeparator);
            }
            else
            {
                double add = 0.5;
                for (int i = 0; i < decimal_places; i++)
                {
                    add *= 0.1f;
                }
                double_val += double_val >= 0 ? add : -add;

                int int_part = (int)double_val;

                if (int_part == 0 && double_val < 0)
                {
                    string_builder.Append('-');
                }

                // First part is easy, just cast to an integer
                string_builder.Concat(int_part, pad_amount, pad_char, 10, thousandSeparator);

                // Decimal point
                string_builder.Append('.');

                // Work out remainder we need to print after the d.p.
                double remainder = Math.Abs(double_val - int_part);
                uint   tmp       = decimal_places;

                // Multiply up to become an int that we can print
                do
                {
                    remainder *= 10;
                    tmp--;
                }while (tmp > 0);

                // Round up. It's guaranteed to be a positive number, so no extra work required here.
                //remainder += 0.5f;

                // All done, print that as an int!
                string_builder.Concat((uint)remainder, decimal_places, '0', 10, thousandSeparator);
            }
            return(string_builder);
        }
 //! Convert a given float value to a string and concatenate onto the stringbuilder. Assumes no padding.
 public static StringBuilder Concat(this StringBuilder string_builder, float float_val, uint decimal_places)
 {
     string_builder.Concat(float_val, decimal_places, 0, ms_default_pad_char, false);
     return(string_builder);
 }
 //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume base ten.
 public static StringBuilder Concat(this StringBuilder string_builder, int int_val, uint pad_amount)
 {
     string_builder.Concat(int_val, pad_amount, ms_default_pad_char, 10, true);
     return(string_builder);
 }
Example #10
0
        //! The worker method. This does a garbage-free conversion of a generic type, and uses the garbage-free Concat() to add to the stringbuilder
        private static void ConcatFormatValue <T>(this StringBuilder string_builder, T arg, uint padding, uint base_value, uint decimal_places, bool thousandSeparation) where T : IConvertible
        {
            switch (arg.GetTypeCode())
            {
            case System.TypeCode.Boolean:
            {
                if (arg.ToBoolean(CultureInfo.InvariantCulture))
                {
                    string_builder.Append("true");
                }
                else
                {
                    string_builder.Append("false");
                }
                break;
            }

            case System.TypeCode.UInt32:
            {
                string_builder.Concat(arg.ToUInt32(NumberFormatInfo.InvariantInfo), padding, '0', base_value, thousandSeparation);
                break;
            }

            case System.TypeCode.Int32:
            {
                string_builder.Concat(arg.ToInt32(NumberFormatInfo.InvariantInfo), padding, '0', base_value, thousandSeparation);
                break;
            }

            case System.TypeCode.Int64:
            {
                string_builder.Concat(arg.ToInt64(NumberFormatInfo.InvariantInfo), padding, '0', base_value, thousandSeparation);
                break;
            }

            case System.TypeCode.UInt64:
            {
                string_builder.Concat(arg.ToInt32(NumberFormatInfo.InvariantInfo), padding, '0', base_value, thousandSeparation);
                break;
            }

            case System.TypeCode.Single:
            {
                string_builder.Concat(arg.ToSingle(NumberFormatInfo.InvariantInfo), decimal_places, padding, '0', false);
                break;
            }

            case System.TypeCode.Double:
            {
                string_builder.Concat(arg.ToDouble(NumberFormatInfo.InvariantInfo), decimal_places, padding, '0', false);
                break;
            }

            case System.TypeCode.Decimal:
            {
                string_builder.Concat(arg.ToSingle(NumberFormatInfo.InvariantInfo), decimal_places, padding, '0', false);
                break;
            }

            case System.TypeCode.String:
            {
                string_builder.Append(arg.ToString());
                break;
            }

            default:
            {
                Debug.Assert(false, "Unknown parameter type");
                break;
            }
            }
        }