By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,567 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Can I use an IFormatProvider to do this?

P: n/a
I have to write to a fixed field file, and I have to format my numbers in a
particular way. I have to front pad them with 0's, and I have to write the
decimal value 3.14 as "314". So for example, if its a 8 character field I'd
have to write:

"00000314"

I looked at MS' formatting documentation (IFormatProvider, IFormatable) but
the docs were complicated and I suspect there's a better explanation for me.
Can someone tell me what's the easiest way to go about this? I'll need to
be able to specify the fixed field width and the number of decimal places.

Jul 8 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"0to60" <ho***************@yahoo.comwrote in message
news:ex**************@TK2MSFTNGP03.phx.gbl...
>I have to write to a fixed field file, and I have to format my numbers in a
particular way. I have to front pad them with 0's, and I have to write the
decimal value 3.14 as "314". So for example, if its a 8 character field
I'd have to write:

"00000314"

I looked at MS' formatting documentation (IFormatProvider, IFormatable)
but the docs were complicated and I suspect there's a better explanation
for me. Can someone tell me what's the easiest way to go about this? I'll
need to be able to specify the fixed field width and the number of decimal
places.
One easy way to do it is String.Format:

decimal n = 3.14m;
string formatted = string.Format("{0:0000000#}", n*100);

By editing the format string and adding additional parameters, you can
format all the values that go into one record for your file in one go.
Jul 8 '08 #2

P: n/a
Alberto Poblacion wrote:
One easy way to do it is String.Format:

decimal n = 3.14m;
string formatted = string.Format("{0:0000000#}", n*100);

By editing the format string and adding additional parameters, you can
format all the values that go into one record for your file in one go.
Perfect!

Now, is there a way to front pad a string with spaces? I need fixed width
strings, and they have to be front padded with spaces.

Jul 8 '08 #3

P: n/a
"0to60" <ho***************@yahoo.comwrote in message
news:eN**************@TK2MSFTNGP04.phx.gbl...
Alberto Poblacion wrote:
> One easy way to do it is String.Format:

decimal n = 3.14m;
string formatted = string.Format("{0:0000000#}", n*100);

By editing the format string and adding additional parameters, you can
format all the values that go into one record for your file in one go.

Perfect!

Now, is there a way to front pad a string with spaces? I need fixed width
strings, and they have to be front padded with spaces.
Nevermind, I got it.

Jul 8 '08 #4

P: n/a
For the people that still want the answer to this.

string newString = oldString.PadLeft(100,' ');

will pad the left hand side up to 100 characters and PadRight will do it to
the right hand side.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"0to60" wrote:
Alberto Poblacion wrote:
One easy way to do it is String.Format:

decimal n = 3.14m;
string formatted = string.Format("{0:0000000#}", n*100);

By editing the format string and adding additional parameters, you can
format all the values that go into one record for your file in one go.

Perfect!

Now, is there a way to front pad a string with spaces? I need fixed width
strings, and they have to be front padded with spaces.

Jul 8 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.