How to get the result of a Javascript BigInt division in significants and exponent
Clash Royale CLAN TAG#URR8PPP
How to get the result of a Javascript BigInt division in significants and exponent
I would like to divide two BigInt numbers and get the result as some significant digits and an exponent. I have got this so far:
f = (numerator, denominator, significantsLength) =>
const denominatorLength = ('' + denominator).length
const extension = significantsLength + denominatorLength
const extendedNumerator = numerator * 10n ** BigInt(extension)
const div = extendedNumerator / denominator
const divLength = ('' + div).length
const digitsSurplus = divLength - significantsLength
const significants = div / 10n ** BigInt(digitsSurplus)
const exponent = digitsSurplus - extension
return significants + 'e' + exponent
so f(1n,3n,3)
results in '333e-3'
f(1n,3n,3)
'333e-3'
Could it be better? This code does not round up. It also does not work well with zero and negative numbers.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.