How to add Trailing zeroes to an integer

前端 未结 4 620
失恋的感觉
失恋的感觉 2021-01-01 05:57

I have a positive integer variable which can have values between 0 to 999. This integer is then passed to a software.

To pass into this software the integer should a

4条回答
  •  遥遥无期
    2021-01-01 06:24

    I don't know why the zeros got chopped off (ideally you should fix the source of the problem), but you can format the numbers as strings and then turn them back into ints:

    numbers = [1, 19, 255]
    numbers = [int('{:<03}'.format(number)) for number in numbers]
    

    This left-aligns each number with <, in a field 3 characters wide, filling extra characters with 0.

提交回复
热议问题