How to add Trailing zeroes to an integer

前端 未结 4 634
失恋的感觉
失恋的感觉 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:36

    You can do this by using ljust and str, then casting the result as an int.

    >>> numbers = [1, 19, 255]
    >>> [int(str(num).ljust(3, '0')) for num in numbers]
    [100, 190, 255]
    

    More on ljust here: https://docs.python.org/2/library/stdtypes.html#string-methods

提交回复
热议问题