snippetcsharpCritical
How can I convert an int to a string and left-pad it with zeros?
Viewed 0 times
withhowzerosandpadintconvertleftcanstring
Problem
In C# I have an integer value which need to be converted to a string, but it needs to add zeros before:
For Example:
When I convert it to string it needs to become
For Example:
int i = 1;When I convert it to string it needs to become
0001Solution
i.ToString().PadLeft(4, '0') - okay, but doesn't work for negative numbersi.ToString("0000"); - explicit formi.ToString("D4"); - short form format specifier$"{i:0000}"; - string interpolation (C# 6.0+)Context
Stack Overflow Q#4325267, score: 929
Revisions (0)
No revisions yet.