.netformattingstring-formattingnumber-formatting

Add comma thousand separator to decimal (.net)


I have a decimal number, say 1234.500.

I want to display it as 1,234.5.

I'm currently converting it to a double to remove the trailing '0's.

string.Format("{0:0,0}",1234.500) removes the decimal place, and other formatting options seem to use two decimal places regardless.

Can anyone offer insight?


Solution

  • You should use a custom formatting like #,##0.00

    string s = string.Format("{0:#,##0.00}", xx);
    

    Will produce 1,234.50 when xx = 1234.5M
    forget about converting to double, that won't really help.