Created
February 1, 2022 19:21
-
-
Save pgebert/be60db0c46252c2b0864c3a53aa9e0ed to your computer and use it in GitHub Desktop.
Get the first and last n digits of an integer number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by pgebert on 01/02/22. | |
* | |
* Get the first and last n digits of an integer number | |
*/ | |
import math | |
def first_n_digits(num: int, n: int): | |
return abs(num) // 10 ** (int(math.log(num, 10)) - n + 1) | |
def last_n_digits(num: int, n: int): | |
return abs(num) % 10 ** n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment