tags:

views:

216

answers:

6

Can someone give me code to do the following....

if the integer is 1-9 display a string 01,02,03 etc.. if 10 or over leave it as is.

string display = yourInt.ToString("00"); 

DOES NOT WORK

+12  A: 
string display = yourInt.ToString("00");
Anthony Pegram
+1 for getting there 10 seconds earlier.
Kyle Rozendo
That doesnt work. That will work of decimals after the deciamls, but its not working for whole numbers
Nick LaMarca
@Nick LaMarca: It does work based on your description of the problem. Hence the upvotes Anthony has received. Could you clarify in what sense it "doesn't work" for you?
Dan Tao
http://ideone.com/tdKAg says it works.
Chris Shouts
@Nick: Decimals after decimals? Not sure what you mean. An integer doesn't have any numbers after the decimal. That's what makes it an integer. Oh, and to support everyone else. "Yes, it does work."
Robaticus
I think it works.In VS 2010 I put int x = 4;string xx = x.ToString("00");Console.WriteLine(xx);And it outputs 04.
abhishek
public static void Main (string[] args){ int num = 9; var text = num.ToString ("00"); Console.WriteLine (text); // outputs 09}
kenny
A: 

Your question is a bit ambiguous: display it where?

In any case, you will probably want to look at String.Format.

Wonko the Sane
+4  A: 

Or, in String.Format syntax (used in Console.WriteLine, for example)

string s = String.Format("{0:00}", yourInt);
Jim Mischel
This doesnt work it does the same thing as ToString("00") which wont add a leading zero
Nick LaMarca
@Nick: Seriously, man, have even tried this or are you just assuming that you know what will happen?
Dan Tao
I just tried it several times it returns for example 1 returns 1, 2 returns 2, 3 returns 3 etc I need 01,02,03
Nick LaMarca
Your results are interesting. I does indeed work as I described. 1 returns "01", as expected. Look at the documentation for Composite Formatting (http://msdn.microsoft.com/en-us/library/txafckwd.aspx), and note that the formatString (the part after the colon) is the same as described for Custom Numeric Format Strings: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx.
Jim Mischel
A: 

In any language (at least the ones i know) and integer value type will never have 2 digits length in any value below 10.

To display it with always a two digits length (as 02, 05, 12) you must convert your number to String and then padding it with 0.

Or you will evaluate it like:

String newValue = (intValue < 10 && intValue > 0 ? "0" + intValue : intValue+"");

As you see you will have to convert it to string before displaying it...

Garis Suero
Yeah thats what I did
Nick LaMarca
I hope `intValue` is never negative.
Chris Shouts
We can work that out!
Garis Suero
+2  A: 

I don't see why .ToString("00") didn't work. This test succeeds...

    [TestMethod]
    public void RightPadIntegersWithZero()
    {
        var values = new[] { -100, -20, -1, 0, 1, 5, 10, 100, 567 };
        var expecteds = new[] { "-100", "-20", "-01", "00", "01", "05", "10", "100", "567" };

        for (var i = 0; i < values.Length; i++)
        {
            var value = values[i];
            var expected = expecteds[i];

            var result = value.ToString("00");
            Assert.AreEqual(expected, result);
        }
    }

You must be doing something different than what your question describes

Chad
A: 

Would this do what you want?

for (int i = -20; i < 100; i++)
{
 string s = i.ToString();
 while (s.Length < 2) s = "0" + s;
 Console.WriteLine(s);
}

I realize it's a bit of a brute force approach as written here. But if it did the trick, you could optimize it with a reusable character array instead of appending the string.

Pat Daburu