473,388 Members | 1,326 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,388 software developers and data experts.

padding a numbers string reprensentation

Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the
numbers get padded with 0's up to a certain amount of characters: For
example I'd like to pad up to 4 characters, then a 17 should look like
this '0017'.

Any help is greatly appreceated.

Matthias
Nov 16 '05 #1
6 3789
I don't recall the specifics, but if you look up standard formatting codes
under String.Format() in the docs you'll find a way to do it with
someInt.ToString(someFormatStringHere).

Another way would be:

int intDesiredLength = 10;
string s = someInt.ToString();
return new String('0',intDesiredLength - intDesiredLength) + s;

--Bob

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:eD*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the
numbers get padded with 0's up to a certain amount of characters: For
example I'd like to pad up to 4 characters, then a 17 should look like
this '0017'.

Any help is greatly appreceated.

Matthias

Nov 16 '05 #2
Sorry, I meant:

return new String('0',intDesiredLength - s.Length) + s;

--Bob

"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I don't recall the specifics, but if you look up standard formatting codes
under String.Format() in the docs you'll find a way to do it with
someInt.ToString(someFormatStringHere).

Another way would be:

int intDesiredLength = 10;
string s = someInt.ToString();
return new String('0',intDesiredLength - intDesiredLength) + s;

--Bob

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:eD*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the
numbers get padded with 0's up to a certain amount of characters: For
example I'd like to pad up to 4 characters, then a 17 should look like
this '0017'.

Any help is greatly appreceated.

Matthias


Nov 16 '05 #3
Lets say you have:
int i=17;
string myString = i.ToString("N1");

This will give you a string that looks like so. "17.0"
This is not what you want, but this is just to show you how it works.
Here is a link that has some other N's so to speak. Try "F4" or something...

http://msdn.microsoft.com/library/de...classtopic.asp

Good luck,
Nick Z.

Matthias S. wrote:
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the
numbers get padded with 0's up to a certain amount of characters: For
example I'd like to pad up to 4 characters, then a 17 should look like
this '0017'.

Any help is greatly appreceated.

Matthias


Nov 16 '05 #4

It's even easier than that:

string s = iWork.ToString().PadLeft(10, '0');

gives a sting representation of an integer 10 digits long
padded left with 0's where iWork is just some integer you
are working with.

Les
-----Original Message-----
I don't recall the specifics, but if you look up standard formatting codesunder String.Format() in the docs you'll find a way to do it withsomeInt.ToString(someFormatStringHere).

Another way would be:

int intDesiredLength = 10;
string s = someInt.ToString();
return new String('0',intDesiredLength - intDesiredLength) + s;
--Bob

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in messagenews:eD*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything in the documentation.

I'd like to change the string representation of an int so that the numbers get padded with 0's up to a certain amount of characters: For example I'd like to pad up to 4 characters, then a 17 should look like this '0017'.

Any help is greatly appreceated.

Matthias

Nov 16 '05 #5
I think the easier implementation would be:

int i = 17;
string s = i.ToString().PadLeft(4, '0')
"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:eD*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the
numbers get padded with 0's up to a certain amount of characters: For
example I'd like to pad up to 4 characters, then a 17 should look like
this '0017'.

Any help is greatly appreceated.

Matthias

Nov 16 '05 #6
string.Format("{0:0000}", 17)

would yield "0017"

-vJ

"Matthias S." <po*****@emvoidSPAMTRAP.de> wrote in message
news:eD*************@TK2MSFTNGP11.phx.gbl...
Hi,

I'm pretty sure this is as easy as it gets, but I couldn't find anything
in the documentation.

I'd like to change the string representation of an int so that the numbers
get padded with 0's up to a certain amount of characters: For example I'd
like to pad up to 4 characters, then a 17 should look like this '0017'.

Any help is greatly appreceated.

Matthias

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Toronto Web Designer | last post by:
I'm having trouble with the padding and margin properties. IE tends to be happier with the padding and Netscape with the margin property. So I tried this: <link href="netscape-styles.css"...
7
by: jmm-list-gn | last post by:
Hello, <http://www.asaom.edu> There are some curious spacing issues with the top bar (gray) and the main navigation bar. The most significant is how the nav bar looks in Opera v6 (win2k): the...
0
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as...
2
by: dvomsaal | last post by:
I seem to remember there is a way to modify an element based on what it is next to, but I can't seem to locate it. Here's my scenario: We have some pages that are built dynamically and present...
3
by: abhivg | last post by:
Hi, I am trying to port a 32 bit Unix application to 64 bit Windows. While compiling on Windows I am getting a number of warnings related to structure padding. More specifically "warning C4820:...
3
by: floppyzedolfin | last post by:
Hi there. I'm coding an encryption / decryption program. At this very moment, I think I should be pretty close from the end, but there's something blocking me on my way. There's a "Padding is...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
6
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
52
by: marc | last post by:
Hello, Is it possible to right pad with "0" (or other character != blank) ? for example : 1 , length 10 ="1000000000" I've tried with sprintf but I can only left pad with "0" or right pad with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.