473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can I use an IFormatProvider to do this?

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
4 2438
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: XmlAdoNewbie | last post by:
Hello All, I am trying to bind a date in the form of "20031218" to a datetime object. I first try to convert the string using Convert.ToDateTime("20031218") but i get an invalid date time...
0
by: Kolmi | last post by:
Has anyone come across a solution to this ? To format a value into an Sql Server sql string, I want to use the call Convert.ToString( value, new MyFormatProvider() ); Why I have to do this...
1
by: vooose | last post by:
Consider an application which has *many* references to DateTime.Parse(string); Is there a way, say when the application first starts up to force this method to use a specific IFormatProvider,...
1
by: Eric Gofoth | last post by:
Hello There doesn't appear to be an iformatprovider for to specify the format to convert a boolean to. i.e. MyBool.ToString("YES/NO") does not work Any workarounds?
3
by: Que | last post by:
Hi The I wrote the following Code If Not txtInvoice.Text = "" Then txtInvoice.Text = Convert.ToInt32(txtInvoice.Text()).ToString("000000") End If
6
by: Andy B | last post by:
I need to take the value of a textbox and format it in a more readable date. How do you do this? I tried textbox.text.tostring("date format string") but the compiler doesnt like that idea... any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.