将int(数字)转换为带有前导零的字符串? (4位数) [英] Convert int (number) to string with leading zeros? (4 digits)
本文介绍了将int(数字)转换为带有前导零的字符串? (4位数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用以下方案将 int
转换为 string
?
How can I convert int
to string
using the following scheme?
-
1
转换为0001
-
123
转换为0123
1
converts to0001
123
converts to0123
当然,字符串的长度是动态的。对于此样本,为:
Of course, the length of the string is dynamic. For this sample, it is:
int length = 4;
如何进行转换?
推荐答案
使用 String.PadLeft 像这样:
var result = input.ToString().PadLeft(length, '0');
这篇关于将int(数字)转换为带有前导零的字符串? (4位数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文