89

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

if (myString.Length < 3)
{
    myString =  "00" + 3;
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
Alex
  • 39,265
  • 21
  • 45
  • 42
  • 1
    Can you clarify why `myString` should become "003"? What is the correlation with the number '3' aside from the intended length? – PinnyM Sep 24 '12 at 18:55
  • Let's have a look at this post : [here in stackoverflow](http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java) – Franziee Sep 24 '12 at 18:38

9 Answers9

162

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");
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 42
    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)` – coeing Jul 29 '13 at 18:17
  • 2
    @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)` – derekantrican Feb 23 '17 at 15:02
  • Or string interpolation `$"This is my number: {number.ToString("D3")}";` – Barry O'Kane Mar 08 '18 at 16:58
  • 5
    Shorter string interpolation `var myString = $"This is my number: {number:D3}";` – pogosama Jul 11 '18 at 07:54
35
 string.Format("{0:000}", myString);
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Haitham Salem
  • 351
  • 3
  • 3
22

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

$"{myString:000}"
Ali
  • 2,574
  • 1
  • 17
  • 24
21

It's called Padding:

myString.PadLeft(3, '0')
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • thats the brute force method, not as the question asked with the format option – user287107 Sep 24 '12 at 18:42
  • 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)... – PinnyM Sep 24 '12 at 18:43
  • `.ToString("format")` and `String.Format("format", value)` is the same: formatting a value with a format string. – user287107 Sep 24 '12 at 18:44
  • 2
    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. – PinnyM Sep 24 '12 at 19:00
9

This is a short hand string format Interpolation:

$"{value:D3}"
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
8

(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

Sxc
  • 223
  • 2
  • 9
  • This introduces an extra, unnecessary step. – Paul Sep 18 '17 at 13:49
  • 1
    @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.) – ToolmakerSteve Feb 19 '18 at 22:48
3

"How to: Pad a Number with Leading Zeros" http://msdn.microsoft.com/en-us/library/dd260048.aspx

user287107
  • 9,286
  • 1
  • 31
  • 47
3

Does it have to be String.Format?

This looks like a job for String.Padleft

myString=myString.PadLeft(3, '0');

Or, if you are converting direct from an int:

myInt.toString("D3");
Sconibulus
  • 732
  • 9
  • 21
3

You can also do : string.Format("{0:D3}, 3);

sstassin
  • 398
  • 1
  • 3
  • 23