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.
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.