Last 2 digits of an integer? Python 3
Asked Answered
L

6

17

With my code, I want to get the last two digits of an integer. But when I make x a positive number, it will take the first x digits, if it is a negative number, it will remove the first x digits.

Code:

number_of_numbers = 1
num = 9
while number_of_numbers <= 100:
  done = False
  num = num*10
  num = num+1
  while done == False:
    num_last = int(repr(num)[x])
    if num_last%14 == 0:
      number_of_numbers = number_of_numbers + 1
      done = True
    else:
      num = num + 1
print(num)
Lilian answered 15/1, 2017 at 18:37 Comment(0)
L
49

Why don't you extract the absolute value of the number modulus 100? That is, use

 abs(num) % 100 

to extract the last two digits?

In terms of performance and clarity, this method is hard to beat.

Lindberg answered 15/1, 2017 at 18:42 Comment(1)
You'll get 8 for 208 and 0 for 200. Make sure that is what you want. A safer solution is str(num)[-2:]Cholent
S
17

To get the last 2 digits of num I would use a 1 line simple hack:

str(num)[-2:]

This would give a string. To get an int, just wrap with int:

int(str(num)[-2:])
Seftton answered 15/1, 2017 at 19:13 Comment(1)
Remember that python has infinite precision integers - converting a 200000 digit number to a string just to get the last few digits is probably not great. If you could, because that's past the default limit for string conversion.Meredi
L
6

Simpler way to extract last two digits of the number (less efficient) is to convert the number to str and slice the last two digits of the number. For example:

# sample function
def get_last_digits(num, last_digits_count=2):
    return int(str(num)[-last_digits_count:])
    #       ^ convert the number back to `int`

OR, you may achieve it via using modulo % operator (more efficient), (to know more, check How does % work in Python?) as:

def get_last_digits(num, last_digits_count=2):
    return abs(num) % (10**last_digits_count)
    #       ^ perform `%` on absolute value to cover `-`ive numbers

Sample run:

>>> get_last_digits(95432)
32
>>> get_last_digits(2)
2
>>> get_last_digits(34644, last_digits_count=4)
4644
Limit answered 15/1, 2017 at 18:40 Comment(2)
Conversion operations are expensive. Why would you want to do this if there are simpler ways?Allemande
@Shiva: Now I have explicitly mentioned that conversion is less efficient. Converison is simpler to implement (atleast as per me)Limit
U
3

to get the last 2 digits of an integer.

a = int(input())
print(a % 100)
Urinary answered 11/10, 2019 at 7:36 Comment(0)
V
1

You can try this:

float(str(num)[-2:])

Vapid answered 7/1, 2021 at 22:38 Comment(0)
P
0

I'd use a regular expression, because I can :)

import re

x = re.sub(r"^\d{2}(\d{2})$", r"\1", str(num))
assert len(str(x)) == 2

If the input is not 4 digits, it sends back the original thing. Maybe the assert could be different. The other reason I like this is that if num is already reduced to 2 char, it just leaves it.

Purr answered 12/8, 2024 at 22:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.