Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format double - thousands separator with decimal but no trailing zeros

Tags:

string

c#

format

I am looking for a format to use with string.Format that would print the following

double d = 123456.123456D;
double d2 = 123456D;

like

123,456.123456
123,456

{0:N6} results in

123,456.123456
123,456.000000

{0:0.######} results in

123456.123456
123456

{0:#.0} results in

123,456
123,456

I cannot workout a format that will get me to what I need. Will I need to define my own format provider ?

like image 892
BuZz Avatar asked Nov 22 '13 13:11

BuZz


People also ask

How do I get rid of trailing zeros?

Multiply by 1. A better way to remove trailing zeros is to multiply by 1 . This method will remove trailing zeros from the decimal part of the number, accounting for non-zero digits after the decimal point. The only downside is that the result is a numeric value, so it has to be converted back to a string.


1 Answers

You're close. Use this format:

{0:#,0.######}
like image 164
D Stanley Avatar answered Sep 20 '22 13:09

D Stanley