Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ouput file in Fixed Width format? 1

Status
Not open for further replies.

damann

Technical User
Jul 19, 2000
114
US
Hey guys,

I am working on a .NET app that needs to output a file in fixed width format (basically every field/column in the file has a "fixed" length/width). Usually my console apps output in comma delimeted format which is easy to string/concat together. But I'm a little stumped on how to accomplish this. Please shed some light on any ideas you may have.

Thanks,
Damann
 
Use string.Format() function to format each field/column in the length you want.
For the char columns use PadLeft() or PadRight() method to pad the output to the desired length:
MyString.PadRight(25,' '); // MyString will be padded up to 25 with spaces
For integer and double fields:
Code:
double price= 1200.39;
int    id=1908;
string sPrice = string.Format("{0:000000000.00}",price); // double with 9 digits and two decimals
string sId = string.Format("{0:00000000}",id); // integer with 8 digits
// Will output: 000001200.39
//              00001908
For date & time use also string.Format()and the format you want:
Code:
DateTime dt = DateTime.Now;
string sDT = dt.ToString("yyyy-MM-dd-hh: mm:ss",System.Globalization.DateTimeFormatInfo.InvariantInfo);
There are many specifier types for Date&Time.
Note that the format is dependent on the regional settings.
obislavu

 
Great! Thanks for the valuable post!


Thanks,
Damann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top