7

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 always be 3 digits. But the problem is, it should have trailing zeroes.

For example:

1 should be passed as 100
19 should be passed as 190
255 should be passed as 255

It can be done by checking the length of the variable and multiplying with 10 or 100 depending on the length. But is there any better Python alternative. Also this doesn't work for value 0.

(Note: Trailing Zeroes is because the integer value is actually a millisecond value. The software reads the values digit by digit and it always needs 3 digits)

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Saravana Murthy
  • 543
  • 2
  • 6
  • 16
  • 9
    Why isn't 1 -> 001? – Reut Sharabani Jan 09 '16 at 00:16
  • Possible duplicate of [Nicest way to pad zeroes to string](http://stackoverflow.com/questions/339007/nicest-way-to-pad-zeroes-to-string) – Reut Sharabani Jan 09 '16 at 00:18
  • @ReutSharabani That question is about padding on the **left**, not the right. – Barmar Jan 09 '16 at 00:18
  • As I have already posted this variable consist a millisecond value and the old software just takes this value and appends it to the "seconds" variable. – Saravana Murthy Jan 09 '16 at 00:18
  • 1
    ... And 1 millisecond added to a second is 1.001 seconds and not 1.100 seconds. – Reut Sharabani Jan 09 '16 at 00:19
  • 1
    @SaravanaMurthy If it's milliseconds, and you append it to seconds, shouldn't it be `1.019`, not `1.190`? – Barmar Jan 09 '16 at 00:19
  • 4
    Integers don't have lengths; string representations of them do. – Scott Hunter Jan 09 '16 at 00:19
  • 2
    Integers aren't read "digit by digit" and they can't be appended to each other. It really sounds like you want a string representation of an integer and that you want padding on the left, not the right. So, `1 should be passed as "001"`. That's why people are confused. – tdelaney Jan 09 '16 at 00:44

4 Answers4

17

You don't even need the formatting operators for this, just plain str methods. To right justify with zeroes:

x.zfill(3)

To left justify with zeroes:

x.ljust(3, '0')

You'd need to wrap x in str first in this scenario if x is currently an int. At that point, it may be worth just using the formatting operators as others have suggested to directly produce the final str, with no intermediate str, when justification is required.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • @TigerhawkT3: True, but there is a big difference between simple single-purpose methods and the whole mini-language that is the formatting codes. :-) – ShadowRanger Jan 09 '16 at 00:30
7

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.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
5

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

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Karen Clark
  • 166
  • 2
  • 4
  • 12
3

As mentioned in the question, milliseconds should be 1 -> 001, not 100. One millisecond is a thousandth of a second, not a tenth.

[str(num).zfill(3) for num in numbers]

or using Tigerhawk's method with the opposite alignment:

["{:>03}".format(num) for num in numbers]
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 3
    I was also confused at how `100` got turned into `1` rather than `001` turning into `1` (as the latter pair is the same number but the former pair is not), but this particular code results in no change to the original `numbers`. – TigerhawkT3 Jan 09 '16 at 00:24
  • Actually if you convert string `'001'` to integer, it will still `1`. – Remi Guan Jan 09 '16 at 00:24
  • 2
    That was another source of confusion for me - the only way to differentiate `1` from `001` is if they're strings, and it seemed that the OP's library seems to want integers (from the OP's description of it). Puzzling indeed. – TigerhawkT3 Jan 09 '16 at 00:32