C# 左右补零

时间:2021-11-26 05:55:06

//不够4位补零

public static string addZero(int val)

{

string str = val + "";

int strLen = str.Length;

if (strLen < 4)

{

while (strLen < 4)

{

StringBuilder sb = new StringBuilder();

sb.Append("0").Append(str);//左补0

//sb.append(str).append("0");//右补0

str = sb.ToString();

strLen = str.Length;

}

}

return str;

}