Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,533 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,789 people online right now. Registration is fast and FREE... Join Now!




how to use format method in c#

 
Reply to this topicStart new topic

how to use format method in c#

umeshshivakanth
20 Aug, 2008 - 03:14 AM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 18

i want numbers in the format "012" if its "12". here's the code but its not working. i am not able to figure out where i am commiting mistake.Plz help

CODE
  
            string sAc;
      string sAcNo =CmbAcNo.SelectedItem.ToString().IndexOf("--").ToString();
            string sAcNo1 = CmbAcNo.SelectedItem.ToString().Substring(0,int.Parse(sAcNo)).ToString();
            sAc = String.Format("{0:000}", sAcNo1).ToString();

        


User is offlineProfile CardPM
+Quote Post

SpiderSpartan
RE: How To Use Format Method In C#
20 Aug, 2008 - 04:05 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
Does seem weird that formatting is not working. Try passing the values as an int instead of a string. That's the only thing that I could get to work.

CODE

int TestInt = 12;
Console.WriteLine("{0:000}",TestInt);


Good luck.
User is offlineProfile CardPM
+Quote Post

djkitt
RE: How To Use Format Method In C#
20 Aug, 2008 - 07:11 AM
Post #3

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
Hey there umeshshivakanth,

As SpiderSpartan pointed out this will only work with a number, not a string. You are trying to use numeric formatting on a string"{0:000}". So, just convert the string to an int first:

CODE

            sAc = String.Format("{0:000}", Convert.ToInt32(sAcNo1)).ToString();


Hope this helps,

Kitt
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:13PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month