将 int(数字)转换为带前导零的字符串?(4 位数字) [英] Convert int (number) to string with leading zeros? (4 digits)
本文介绍了将 int(数字)转换为带前导零的字符串?(4 位数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
数字格式:如何将 1 转换为01"、2 转换为02"等?
如何使用以下方案将 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;
我怎样才能像这样转换?
How can I convert like it?
推荐答案
使用 String.PadLeft 像这样:
var result = input.ToString().PadLeft(length, '0');
这篇关于将 int(数字)转换为带前导零的字符串?(4 位数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文