object Main {
/**
* Returns the N-th digit of a BigInt counting from the left.
*
* Examples:
* getNthDigitFromLeftBigInt(BigInt("12345"), 1) = 1
* getNthDigitFromLeftBigInt(BigInt("12745"), 3) = 7
* getNthDigitFromLeftBigInt(BigInt("12345"), 5) = 5
*/
def getNthDigitFromLeftBigInt(inputNumber: BigInt, digitPosition: Int): Int = {
require(
digitPosition > 0,
"Digit position must be greater than 0."
)
// Convert the absolute value to a string.
val digitString = inputNumber.abs.toString
require(
digitPosition <= digitString.length,
"Digit position is out of range."
)
// Extract and convert the digit character.
digitString.charAt(digitPosition - 1).asDigit
}
def main(args: Array[String]): Unit = {
val largeNumber = BigInt("123456789702345678908234567890")
println(
getNthDigitFromLeftBigInt(largeNumber, 10)
)
}
}
/*
run:
7
*/