From 405a8ee3ed7883d1596c68bc43d19db78c976923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Wed, 1 Mar 2023 17:45:08 +0100 Subject: [PATCH] fix(LocaleUtils): Improve handling small numbers by fractionalPartLength(num) Closes: #9681 --- ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml index d451fc4cb9..af91f4f5eb 100644 --- a/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml +++ b/ui/StatusQ/src/StatusQ/Core/LocaleUtils.qml @@ -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) {