Format string to a 3 digit number

168,455

Solution 1

If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly:

myString = 3.ToString("000");

Or, alternatively, use the standard D format string:

myString = 3.ToString("D3");

Solution 2

 string.Format("{0:000}", myString);

Solution 3

It's called Padding:

myString.PadLeft(3, '0')

Solution 4

This is how it's done using string interpolation C# 7

$"{myString:000}"

Solution 5

(Can't comment yet with enough reputation , let me add a sidenote)

Just in case your output need to be fixed length of 3-digit , i.e. for number run up to 1000 or more (reserved fixed length), don't forget to add mod 1000 on it .

yourNumber=1001;
yourString= yourNumber.ToString("D3");        // "1001" 
yourString= (yourNumber%1000).ToString("D3"); // "001" truncated to 3-digit as expected

Trail sample on Fiddler https://dotnetfiddle.net/qLrePt

Share:
168,455

Related videos on Youtube

Dad Daniel
Author by

Dad Daniel

Updated on March 28, 2020

Comments

  • Dad Daniel
    Dad Daniel about 4 years

    Instead of doing this, I want to make use of string.format() to accomplish the same result:

    if (myString.Length < 3)
    {
        myString =  "00" + 3;
    }
    
    • Franziee
      Franziee over 11 years
      Let's have a look at this post : here in stackoverflow
    • PinnyM
      PinnyM over 11 years
      Can you clarify why myString should become "003"? What is the correlation with the number '3' aside from the intended length?
  • user287107
    user287107 over 11 years
    thats the brute force method, not as the question asked with the format option
  • PinnyM
    PinnyM over 11 years
    Is there a scenario where that would yield a different result? Technically .ToString() isn't directly using String.Format() either (although it will under the hood)...
  • user287107
    user287107 over 11 years
    .ToString("format") and String.Format("format", value) is the same: formatting a value with a format string.
  • PinnyM
    PinnyM over 11 years
    It isn't clear from the question that the formatting should always be applied to a static number (why the OP chose to use '3' is still unclear to me, hence my question above). It may well be some other characters and can't use the numeric formatters. Padding is a safe alternative in such a case.
  • coeing
    coeing almost 11 years
    If you want to use the formatted number directly in a constructed string, you can also use: string.Format("This is my number: {0:D3}", number)
  • derekantrican
    derekantrican about 7 years
    @coeing realize that this will only work when number is an int. If number is a double, for instance, it will have to be string.Format("{0:000}", number)
  • Paul
    Paul over 6 years
    This introduces an extra, unnecessary step.
  • ToolmakerSteve
    ToolmakerSteve about 6 years
    @Paul - He is saying that IF the input might be > 1000, and the requirement is that the output always be EXACTLY 3 characters, then one way to meet that requirement is to force the incoming number to fit within 3 digits; e.g. MOD. As the comments on his code lines show, just using "D3" will output 4 characters for the value 1001. So instead, for this usage scenario, use the final line. (For example, I have used a similar technique in debugging output, when the value is part of a complex object that I am printing details of, and I want details to line up, ok to miss some high digits.)
  • Barry O'Kane
    Barry O'Kane about 6 years
    Or string interpolation $"This is my number: {number.ToString("D3")}";
  • pogosama
    pogosama almost 6 years
    Shorter string interpolation var myString = $"This is my number: {number:D3}";