I need to pad the output of an integer to a given length.
For example, with a length of 4 digits, the output of the integer 4 is "0004" instead of "4". How can I do this in C# 2.0?
Use the string.Format command.
output = String.Format("{0:0000}", intVariable);
More details: http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
i think it was: intVal.ToString( "####" );
but there is some useful documentation here
In modern .NET 5.0+ (2021 update)
int myint = 100;
string zeroPadded = $"{myint:d8}"; // "00000100"
string leftPadded = $"{myint,8}"; // " 100"
string rightPadded = $"{myint,-8}"; // "100 "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.