how to output a number like "20.00"

  • Thread starter Thread starter Stimp
  • Start date Start date
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
 
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
 
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! :)
 
There are many ways to string.Format:

string.Format("{0:###,###,###,##0.00;-#,###,###,##0.00;0.00}", YourValue);
 
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.
 
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.
 
Back
Top