PC Review


Reply
Thread Tools Rate Thread

Can I use an IFormatProvider to do this?

 
 
0to60
Guest
Posts: n/a
 
      8th Jul 2008
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.

 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      8th Jul 2008
"0to60" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
0to60
Guest
Posts: n/a
 
      8th Jul 2008
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.

 
Reply With Quote
 
0to60
Guest
Posts: n/a
 
      8th Jul 2008
"0to60" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      8th Jul 2008
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.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use an IFormatProvider to do this? 0to60 Microsoft Dot NET Framework 4 8th Jul 2008 05:18 PM
IFormatProvider Que Microsoft VB .NET 3 18th Sep 2006 08:41 PM
iformatprovider for boolean =?Utf-8?B?RXJpYyBHb2ZvdGg=?= Microsoft VB .NET 1 19th Apr 2004 08:37 PM
Date Time and IFormatProvider XmlAdoNewbie Microsoft Dot NET 2 26th Feb 2004 02:25 PM
IFormatProvider for non-custom format Problem Kolmi Microsoft C# .NET 0 19th Dec 2003 12:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:42 PM.