peter
peter

Reputation: 2103

String format: leading zeroes and no trailing zeroes

I'd like to format a double like this:

1.23 => 1.2
1.0  => 1
0.4  => 0.4
0    => 0

What is the corresponding string format? I'm currently using

StringFormat={}{0:#.#}

which is nice, since it leaves out a trailing zero. Unfortunatly, it turns

0   => ""
0.4 => .4

I've googled for quite some time now and find it pretty hard to find a needed string format. Would I have to adjust the culture format?

Thank you for any help!

Upvotes: 1

Views: 103

Answers (2)

user5464541
user5464541

Reputation: 15

Tested Ok!

decimal d = Convert.ToDecimal("5.23");    
string sOutpu = d.ToString("0.#");

Upvotes: 0

D Stanley
D Stanley

Reputation: 152491

To preserve leading zeros use

StringFormat={}{0:0.#}

However, I'm confused by the translation 1.23 => 1.3. If you meant 1.23 => 1.2 then the format string above should work.

Upvotes: 8

Related Questions