Adding a 0 if number is less than 10

13,510

Solution 1

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));
    }

Solution 2

You don't need IF, use ToString

int i = 5;

i.ToString("00"); //returns 05

Solution 3

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

Solution 4

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.

Solution 5

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);
Share:
13,510
Peter
Author by

Peter

I recently graduated from an intensive Applications Development course where I earned two Microsoft certifications, including MCTS 3.5 ASP.NET. I am now beginning a career in web and application development.

Updated on June 24, 2022

Comments

  • Peter
    Peter almost 2 years

    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!