Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:14:57+00:00 2026-06-11T13:14:57+00:00

Possible Duplicate: C# int ToString format on 2 char int? Sorry for the simplicity,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:14:58+00:00Added an answer on June 11, 2026 at 1:14 pm

    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));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how to parse hex or decimal int in Python i have a
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Possible Duplicate: Difference between int[] array and int array[] I was sure that this
Possible Duplicate: Char to int conversion in C. I remember learning in a course
Possible Duplicate: Destructors of builtin types (int, char etc..) Template Function: template<typename T> void
Possible Duplicate: convert string list to int list in haskell I have a string
Possible Duplicate: Convert ascii to int ?? int a=53 This is an ASCII value
Possible Duplicate: Is this possible in C++? toString(ClassName* class) I'm trying to use toString
Possible Duplicate: Passing a Int value to another class I have been trying to
Possible Duplicate: Why can Integer and int be used interchangably? I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.