How to get the result of a Javascript BigInt division in significants and exponent

The name of the pictureThe name of the pictureThe name of the pictureClash 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.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS