// Extracts the first n digits from a BigInt.
// Converting the BigInt to a string is the most direct and efficient way
// to access its digits without performing repeated arithmetic operations.
function firstNDigits(value, n) {
// Convert the BigInt to its decimal string representation.
const s = value.toString();
// If n exceeds the number of digits, return the whole number.
if (n >= s.length) {
return s;
}
// Return the first n characters (digits).
return s.slice(0, n);
}
function main() {
// Define a very large BigInt directly using the BigInt literal syntax.
const bigNumber = 9876543210987654321098765432109876543210n;
// Choose how many digits to extract.
const n = 14;
// Extract the first n digits.
const result = firstNDigits(bigNumber, n);
// Display the result.
console.log(`First ${n} digits: ${result}`);
}
main();
/*
run:
First 14 digits: 98765432109876
*/