Click here to Skip to main content
15,886,770 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I am struggling to work out how to pad an integer field with leading zeroes in the new C# interpolated strings.

According to this article C#6: String Interpolation, "The same composite formatting power is there to specify things like significant figures and leading zeroes."

In this same article is a link to this page: Standard Numeric Format Strings[^]. According to this page, something like "D2" should use two digits for an integer and pad with zeroes. When I try it, it seems to do nothing.

E.g.

C#
int x = 3;
Console.WriteLine( $"Integer padded to two places: {x:D2}." );

This prints "3", not "03".

Does anyone know how to do this?

Kind wishes ~ Patrick

PS - Judging by discussion below, it seems I have not been 100% clear about what I am trying to achieve.

I know the numbers will always be in the range 0 <= x <= 99. When I get an input in the range 0-9, I want it to add one leading zero (e.g. 4 prints "04", but 14 prints "14"). Hope this clarifies the question.

What I have tried:

In addition to the code shown above, I have tried "##", "DD" and a couple of other variants that I have forgotten.
Posted
Updated 28-Apr-17 9:33am
v2
Comments
Richard Deeming 30-Mar-16 12:21pm    
Your code prints 03 for me.

What output do you get using the older syntax?
Console.WriteLine("X = {0:D2}", x);
Mario Z 30-Mar-16 13:03pm    
Same as Richard, I get the expected "Integer padded to two places: 03."
What version of VS are you using? It could be a version issue, are you perhaps using VS 2013 and you downloaded the Roslyn by yourself, in which case it's possible you're using an older compiler.
Patrick Skelton 30-Mar-16 13:35pm    
Mmm... maybe it is a compiler issue. I am actually in a Xamarin.iOS application, which I think may well use the Roslyn compiler.

If I try this:

int x = 3;
string s = String.Format( "{0:D2}", x );
Console.WriteLine( s );

I still don't get a padded integer.
Richard Deeming 30-Mar-16 16:57pm    
Sounds like it's a bug / "feature" in Xamarin.iOS; your code works fine in a standard .NET application.

Not exactly C# 6, but simple:
string x = "3";
Console.WriteLine(x.PadLeft(2, '0'))
 
Share this answer
 
Comments
Patrick Skelton 30-Mar-16 13:39pm    
This pads the integer, but using a space character, not a leading zero (for me anyway - see comment about compiler above).
Sergey Alexandrovich Kryukov 30-Mar-16 16:38pm    
This solution is so wrong!
I want you to understand it.
Say, you have number 3 and 13. You will have
"003" and
"0013", output of different width. But it's pretty obvious that the inquirer really needs fixed-width output:
"003"
"013", and so on...
—SA
RickZeeland 31-Mar-16 2:12am    
Strange, when I test with .NET 4.52 it works as it should, read about it here if you don't believe it: https://msdn.microsoft.com/en-us/library/92h5dc07(v=vs.110).aspx
Sergey Alexandrovich Kryukov 31-Mar-16 2:23am    
You don't understand. There is no need to test, your method works as you expected, but what you expected is not what is required.

The requirement is padding, which means, for example (pseudo-code):

if input in 0..9, pad 3 '0'
if input in 10..99, pad 2 '0'
if input in 100...999 pad 1 '0'
else no padding

Can you see the idea? In all ranges, the length should be the same, 4 digits (for example). And this is solved by just one string.Format, using Custom Format with '0' placeholders, for example.

—SA
RickZeeland 31-Mar-16 3:04am    
Just test it, you will see the length of the string will stay the same.
"D" or "d" in specifier found in "Standard Numeric Format Strings" is not it. It is not leading/padding, this is "Precision specifier: Minimum number of digits". Can you see the difference? Here is an additional explanation: Standard Numeric Format Strings[^].

For leading digits or padding, you need "Custom Numeric Format Strings": Custom Numeric Format Strings[^].

Pay attention for the first line, the specifier "0", see also "#". Play with it a bit; and you will quickly see what it really means.

—SA
 
Share this answer
 
Comments
Matt T Heffron 30-Mar-16 17:34pm    
From the Standard Numeric Format Strings link you provided, "D2" will pad 0-9 with a leading 0, >= 10 will not pad.
If the OP knows that their values are all <= 99, then "D2" is a perfectly valid way to accomplish output with a fixed width of 2 chars.
Sergey Alexandrovich Kryukov 30-Mar-16 20:58pm    
You are right, but the inquirer is not happy, and usually it happens when people want exact fixed length of output string for certain range of numbers giving different lengths otherwise...
—SA
A concrete example:
C#
var number = 123;
Console.WriteLine($"{number:000000}");

// 000123
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900