C# - increment number and keep zeros in front

前端 未结 5 605
再見小時候
再見小時候 2021-02-07 10:17

I need to make a 40 digit counter variable. It should begin as 0000000000000000000000000000000000000001
and increment to
000000000000000000000000000

5条回答
  •  逝去的感伤
    2021-02-07 11:20

    Just convert your string to int, perform the addition or any other operations, then convert back to string with adequate number of leading 0's:

    // 39 zero's + "1"
    string initValue = new String('0', 39) + "1";
    
    // convert to int and add 1
    int newValue = Int32.Parse(initValue) + 1;
    
    // convert back to string with leading zero's
    string newValueString = newValue.ToString().PadLeft(40, '0');
    

提交回复
热议问题