fix(LocaleUtils): Improve handling small numbers by fractionalPartLength(num)

Closes: #9681
This commit is contained in:
Michał Cieślak 2023-03-01 17:45:08 +01:00 committed by Michał
parent a2a1da801c
commit 405a8ee3ed
1 changed files with 22 additions and 3 deletions

View File

@ -10,9 +10,28 @@ QtObject {
if (Number.isInteger(num))
return 0
let parts = num.toString().split('.')
// Decimal trick doesn't work for numbers represented in scientific notation, hence the hardcoded fallback
return (parts.length > 1 && parts[1].indexOf("e") == -1) ? parts[1].length : 2
// According to the JS Reference:
//
// Scientific notation is used if the radix is 10 and the number's
// magnitude (ignoring sign) is greater than or equal to 10^21 or less
// than 10^-6. In this case, the returned string always explicitly
// specifies the sign of the exponent.
//
// In order to take it into account, numbers in scientific notation
// is handled separately.
if (Math.abs(num) < 10**-6) {
const split = num.toString().split('e-')
const base = parseFloat(split[0])
const exp = parseInt(split[1])
return fractionalPartLength(base) + exp
}
if (num >= 10**21) {
return 0
}
return num.toString().split('.')[1].length
}
function stripTrailingZeroes(numStr, locale) {