Search code examples
c#type-conversiondoubledecimaltrailing

C# Convert string to double/decimal and back to string, keeping trailing zeroes, adding comas for thousands


I am trying to get user input, parse it and then display with String.Format(), formatting thousands with comas.

So, if user provides
1000  I will display 1,000
1000.00 => 1,000.00
1000.0  => 1,000.0
1,000.5 => 1,000.5

Basically I want to keep all decimals(including trailing zeroes) that were provided and just add formatting for thousands. I tried:

String.Format("{0:#,0.######}" , Decimal.Parse(input));
String.Format("{0:#,0.######}" , Double.Parse(input);

Solution

  • double.Parse(input) is a no go, as double does not keep track of the number of decimals.

    decimal.Parse(input).ToString() will show that decimal does keep track of that. Unfortunately, decimal.Parse(input).ToString() uses this precision and doesn't use a thousands separator, and decimal.Parse(input).ToString("N") ignores the precision but does use a thousands separator.

    Manually extracting the precision from the decimal works though, and that allows you to build the correct format string:

    static string InsertThousandsSeparator(string input) {
        var dec = decimal.Parse(input);
        var bits = decimal.GetBits(dec);
        var prec = bits[3] >> 16 & 255;
        return dec.ToString("N" + prec);
    }
    

    This is based on the layout of decimal as described on MSDN:

    Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.

    See it working on .NET Fiddle. (courtesy of @Alisson)