PC Review


Reply
Thread Tools Rate Thread

3 -> 0003 Simple String.Format Question!

 
 
Cat
Guest
Posts: n/a
 
      28th Sep 2005
Hello.
I have an integer whose value varies from 0 to 9999.
I want to print it in a four-digit format.
Like,
0000
0001
0002
.....
9998
9999
I tried String.Format("{0:####}",val), but it didn't work. How can I do
this? Thanks.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?QnJlbmRhbiBHcmFudA==?=
Guest
Posts: n/a
 
      28th Sep 2005
Instead of: String.Format("{0:####}",val),

Try: val.ToString("0000")

Brendan

"Cat" wrote:

> Hello.
> I have an integer whose value varies from 0 to 9999.
> I want to print it in a four-digit format.
> Like,
> 0000
> 0001
> 0002
> .....
> 9998
> 9999
> I tried String.Format("{0:####}",val), but it didn't work. How can I do
> this? Thanks.
>
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      28th Sep 2005
Cat <(E-Mail Removed)> wrote:
> I have an integer whose value varies from 0 to 9999.
> I want to print it in a four-digit format.
> Like,
> 0000
> 0001
> 0002
> ....
> 9998
> 9999
> I tried String.Format("{0:####}",val), but it didn't work. How can I do
> this? Thanks.


Try {0:0000} as the format string instead. Alternatively (and slightly
more compactly), you can call ToString directly on val:
val.ToString("0000")

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Bill Butler
Guest
Posts: n/a
 
      28th Sep 2005

"Cat" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello.
> I have an integer whose value varies from 0 to 9999.
> I want to print it in a four-digit format.
> Like,
> 0000
> 0001
> 0002
> ....
> 9998
> 9999
> I tried String.Format("{0:####}",val), but it didn't work. How can I do
> this? Thanks.
>


String.Format("{0:d4}",val);

Bill


 
Reply With Quote
 
Chris Parkinson
Guest
Posts: n/a
 
      28th Sep 2005
> I tried String.Format("{0:####}",val), but it didn't work. How can I
do
> this? Thanks.


String.PadLeft(4, '0')

--
Chris Parkinson


 
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
Simple String Question pete3589 Microsoft Excel Programming 3 22nd Aug 2006 03:28 PM
Simple string question =?Utf-8?B?RS1DdWJl?= Microsoft C# .NET 2 20th Dec 2004 03:17 AM
Simple string question Ammar Microsoft ASP .NET 2 3rd Jul 2004 05:08 PM
Simple String.Format method question Burak Microsoft VB .NET 5 16th Jun 2004 05:35 PM
Re: Simple string question.... JohnFol Microsoft Access Form Coding 0 12th Aug 2003 09:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 PM.