Format string to a 3 digit number

前端 未结 9 1338
说谎
说谎 2020-12-15 02:07

Instead of doing this, I want to make use of string.format() to accomplish the same result:

if (myString.Length < 3)
{
    myString =  \"00\"         


        
9条回答
  •  情歌与酒
    2020-12-15 02:48

    If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly:

    myString = 3.ToString("000");
    

    Or, alternatively, use the standard D format string:

    myString = 3.ToString("D3");
    

提交回复
热议问题