Rene
Rene

Reputation: 3

How do you convert a double to a formatted string?

I searched a lot about this problem but the only results I get is with numbers like 5.04 My number is >1 so for example 0.8 number.ToString("F2") is the way I tried but haven't found a format yet that worked. So Do anybody know how to show a double number in a label?

Upvotes: 0

Views: 10307

Answers (4)

artwl
artwl

Reputation: 3582

Formatted two decimal places:

String.Format("{0:0.00}", 123.4567);      // "123.46"

String.Format("{0:0.00}", 123.4);         // "123.40"

String.Format("{0:0.00}", 123.0);         // "123.00"

Upvotes: 0

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74370

Not sure what you are asking, but what's wrong with:

string formatted = string.Format("{0:F2}",0.8);

It produces 0.80, which I thought is what you were looking for.

Upvotes: 1

Dave
Dave

Reputation: 15016

Have you tried:

string s = String.Format("{0:0.00}", 0.8);

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101604

The article on MSDN may be of help regarding formatting a Decimal number in a variety of formats. Scroll a bit down and view the examples given and their output they would provide.

Upvotes: 2

Related Questions