how to output a number like "20.00"

S

Stimp

I want to output a number, say 20, as 20.00

i.e. I want to always have 2 decimal places

I don't want any currency information, therefore String.Format("{0:c}")
doesn't work for me.

Can't seem to find a way to do this. Any ideas?

Thanks,
Peter
 
B

Basil Abusamrah

Stimp,

Use the format 'f'

Example:

int myInt = 24;
Console.WriteLine("My Int : {0:f}", myInt);

Should output:

My Int : 24.00

Hope that helps, Basil
 
S

Stimp

Stimp,

Use the format 'f'

Example:

int myInt = 24;
Console.WriteLine("My Int : {0:f}", myInt);

Should output:

My Int : 24.00

Hope that helps, Basil

that worked great. thanks! :)
 
N

newscorrespondent

There are many ways to string.Format:

string.Format("{0:###,###,###,##0.00;-#,###,###,##0.00;0.00}", YourValue);
 
S

Stimp

There are many ways to string.Format:

string.Format("{0:###,###,###,##0.00;-#,###,###,##0.00;0.00}", YourValue);

That actually doesn't work for what I needed though. I had tried it.

#.## only outputs 20 instead of "20.00" or 20.1 instead of "20.10" etc.

{0:f} is the best way to go.
 
L

Lau Lei Cheong

it's not #.## but #0.00, through.

# is a optional digits placeholder(i.e.: has output only when it's a
significant digit), while 0 is arbitary (i.e.: it has output regardless of
whether it's significant or not.)

P.S.: the above paragraph requires you to treat 20 as ...0000020.0000...
where the empty place is in fact filled with 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,437
Messages
2,571,697
Members
48,796
Latest member
Greg L.
Top