Skip to content

Instantly share code, notes, and snippets.

@pgebert
Created February 1, 2022 19:21
Show Gist options
  • Save pgebert/be60db0c46252c2b0864c3a53aa9e0ed to your computer and use it in GitHub Desktop.
Save pgebert/be60db0c46252c2b0864c3a53aa9e0ed to your computer and use it in GitHub Desktop.
Get the first and last n digits of an integer number.
/**
* 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