字符串格式

问题描述:

嗨.
如果用户插入数字小于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);