I am trying to take air pressure readings and plot them with their last 2 digits showing. However, when I use modulo:
--> x = 1004
--> x % 100
--> 4
This just generates 4. How can I display this to show 04 instead of 4?
Thanks!
I am trying to take air pressure readings and plot them with their last 2 digits showing. However, when I use modulo:
--> x = 1004
--> x % 100
--> 4
This just generates 4. How can I display this to show 04 instead of 4?
Thanks!
If you need it as an int or a float, I don't think this is possible. If it's only for display purposes, convert it to a string and take the last 2 characters:
x = 1004
>>> str(x)[-2:]
'04'
If you just print out a number, it prints it out in its default format. For the number 2
, that's obviously going to be 2
, not 02
.
If you want to specify a custom format, you need to use some form of string formatting. Python has a few ways to do it.
The same Format String Syntax is used by the format
function, the str.format
method, Formatter
subclasses, and (with slight differences that aren't relevant here) f-string literals.
You can specify a width
of 2
, plus an align
of =
(meaning numeric alignment—padding is placed after any +
or -
) and a fill
of 0
. But there's also a special shortcut, where placing a 0
right before the width
means numeric-aligned zero-fill. So:
>>> f"{x % 100:02}"
'02'
>>> format(x % 100, '02')
'02'
>>> '{:02}'.format(x % 100)
'02'
printf
-style String FormattingPython has an older, but still sometimes useful, way to do formatting,1 which more closely matches that of C and similar languages, calling printf
-style or %
-formatting.
You specify a width
of 2
, and a flag
of 0
, which indicates numeric zero-padding, together with a type
of d
to specify that you want to format the number as a signed integer decimal:
>>> "%02d" % (x % 100,)
'02'
While %
-formatting isn't as flexible and powerful as format strings, it can sometimes be simpler to understand (especially if you're used to C or another language), is often faster, and works with bytes
as well as str
.
Finally, you can always convert the number to a string and then use string methods on it. For example, you can use the zfill
method to zero-fill a string:
>>> str(x % 100).zfill(2)
'02'
… or you can use the rjust
method to right-justify it with '0'
as a fill character:
>>> str(x % 100).rjust(2, '0')
'02'
In fact, instead of calculating the remainder, you could just convert the whole thing to a string, truncate it, then zero-fill:
>>> str(x)[-2:].zfill(2)
… although this probably won't be what you want is x
is, say, -123
(you'll get 23
instead of 77
).
1. In fact, it provides two older solutions, the other being template strings, but these aren't useful as often.
You could use string formatting too.
ans = 1004 % 100
format(ans, '03d')
More information about String formatting can be found here: https://pyformat.info/
x = 1004
x = x % 100
str(x)
y = len(x)
if y == 0:
end
elif y == 2:
print (x)
elif y == 1:
print ('0'x)
else:
x = str(x)[-2:]
print (x)
In this, I've converted it into a string & calculated the length of the value as 'y'. Using a basic [if elif else] structure I have returned the values in your format, but as strings.
It is unclear what you wanted to use these values for because if you want them to represent figures you will probably need to edit the settings of whatever output application you are using. An alternative solution would be to use the value for 'x' as being different to the string so that the number value can still be used regardless of the way it is displayed when printing. wink, wink.
No modulo, no zfill.
"{:02d}".format(12345)[-2:]
#> 45
"{:02d}".format(0)[-2:]
#> 00
"{:02d}".format(-9876)[-2:]
#> 76
"{:02d}".format(-1)[-2:]
#> -1
Your first step to use the modulus operator to find the two-least significant digits is correct.
Then you'll want to use a format string to pad your result when printing it. See: Python format Integer into fixed length strings