字符串格式 [英] String.Format
本文介绍了字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨.
如果用户插入数字小于10,我想在数字的左侧添加一个零.
Hi.
I wanna to add a zero at the left of a number if the number is less than 10, when the users insert it.
01
02
03
04
您能指导我,如何使用String.Format
做到这一点.
Could you guide me, how I can do it with String.Format
.
Thanks.
推荐答案
您可以在数字类型的ToString方法中指定格式.一个int:
You can specify a format in the ToString method of the numeric type.
i.e. an int:
int number = 2;<br />string numberString = number.ToString("00");
static string Method1(int i)
{
return i.ToString().PadLeft(2, '0');
}
或者,您可以执行以下操作:
Or, you can do this:
string myString = string.Format("{0:00}", number);
这篇关于字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文