StreamWriter: How to format a decimal?

  • Thread starter Thread starter David Rose
  • Start date Start date
D

David Rose

Hi All,

This has to be simple, I feel foolish asking, but...

How do you format a decimal with a StreamWriter? I would like to write a
decimal value to a text file in the format xxxxx.xxxx (like 2 would be
2.0000 - don't need to prepend zeros).

Never thought I would say it, but I'm starting to miss printf().

David
 
To answer my own question, I can use ToString()...

double x = 0.000;
writer.WriteLine("{0,-10}", x.ToString("#####.0000"));
writer.WriteLine("{0,10}", 77.ToString("#####.0000"));

But... This is kinda cumbersome. Is there a better way?

David
 
Hi, David Rose,

You were quite close. Try:

writer.Write("{0:#####.0000}", x);

Greetings
Martin
 
Back
Top