6

Possible Duplicate:
C# int ToString format on 2 char int?

Sorry for the simplicity, but this one is eluding me. Pretty much I have a list of 36 records, and if the id is less than 10, I need it to return 01, 02, 03... 09, instead of 1, 2, 3... 9.

Here is the code I have so far and I would have thought this would work. This is C# .NET:

for (int i = 1; i <= 36; i++)
{
    if (i.ToString().Length == 1)
    {
        i.ToString().PadLeft(2,'0');
    }

    Response.Write("Test: " + i);
}

Any help would be appreciated, thanks in advance!

Community
  • 1
  • 1
Peter
  • 427
  • 4
  • 9
  • 37
  • 2
    Close, but you're just throwing the result away. – harold Sep 18 '12 at 15:47
  • See, I don't like closing this while becaues the question was about the padding, the issue the OP was having had nothing to do with that, ironically he was doing that correct, he just wasn't assigning the value properly. – CaffGeek Sep 18 '12 at 16:01
  • @CaffGeek the problem is no one with the same problem will ever see the right answer because the question is not asking right thing. It probably could get salvaged to reask the proper question, but then you potentially invalidate at least 1 answer that didn't address the OP's real problem – psubsee2003 Sep 18 '12 at 16:09

6 Answers6

22

You don't need IF, use ToString

int i = 5;

i.ToString("00"); //returns 05
Alberto León
  • 2,879
  • 2
  • 25
  • 24
14

Your problem is i is still an integer, it needs to be assigned to a string

  for (int i = 1; i <= 36; i++)
    {
        var iString = i.ToString();

        if(iString.Length == 1)
        {
            iString = iString.PadLeft(2,'0'); //RIGHT HERE!!!
        }
        Response.Write("Test: " + iString);
    }

However, much of this code is superflous, the if statement is not needed. Pad will only ped with zeroes up to the length (2) given. If it's already 2 or more characters long, it won't pad anything. All you need is this

    for (int i = 1; i <= 36; i++)
    {
        var iString = i.ToString().PadLeft(2,'0');
        Response.Write("Test: " + iString);
    }

For that matter, the variable is no longer needed.

    for (int i = 1; i <= 36; i++)
    {
        Response.Write("Test: " + i.ToString().PadLeft(2,'0'));
    }

And if you'll be padding with zeroes all the time, and not some other character, you could just do this

    for (int i = 1; i <= 36; i++)
    {
        Response.Write("Test: " + i.ToString("00"));
    }

And you should get into the habit of using string.Format

    for (int i = 1; i <= 36; i++)
    {
        Response.Write(string.Format("Test: {0}", i.ToString("00")));
    }

And to simplify the string.Format even further:

    for (int i = 1; i <= 36; i++)
    {
        Response.Write(string.Format("Test: {0:00}", i));
    }
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
4

You can try with

var list = new List<string>();
for (int i = 1; i <= 36; i++)
{
    var result = string.Empty; 
    if(i < 10)
    {
         result = string.Format("0{0}", i);
    }
    else
    {
        result = i.ToString();
    }
    list.Add(result);
}

Nota : Concat your values nefore call Response.Redirect

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
3

You are not doing anything with the following line:

i.ToString().PadLeft(2,'0');

i is still just an integer, and its string representation is not going to have a 0 in front of it when you use it later. You would need to save a string of the value to print later.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
3

You needn't check i.ToString().Length == 1:

for (int i = 1; i <= 36; i++)    
    Response.Write("Test: " + i.ToString().PadLeft(2,'0')); 

Look to PadLeft in MSDN for clarification. Common signature:

public string PadLeft(int totalWidth, char paddingChar)

Also you can use String.Format:

for (int i = 1; i <= 36; i++)    
    Response.Write("Test: " + i.ToString("00")); 

Another way - use LINQ:

foreach (var number in Enumerable.Range(1, 36).Select(i => i.ToString("00")))
  Response.Write("Test: " + number);
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
2

You're simply converting i to string and throwing away the result. In Response.Write("Test: " + i); you're printing i instead of its conversion to string. Try:

for (int i = 1; i <= 36; i++)
{
    if (i.ToString().Length == 1)
    {
           Response.Write(i.ToString().PadLeft(2,'0'));
    }
}
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86