From 7377db8d280a90515fe099cb580607450cb146a5 Mon Sep 17 00:00:00 2001 From: Ibrahim <93064150+IbrahimCSAE@users.noreply.github.com> Date: Thu, 15 Aug 2024 11:13:27 -0400 Subject: [PATCH] fix(roundNumber): handle negative numbers properly (#4336) --- platform/core/src/utils/roundNumber.js | 39 ++++++++++++++++---------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/platform/core/src/utils/roundNumber.js b/platform/core/src/utils/roundNumber.js index be76c7ede..64f614a86 100644 --- a/platform/core/src/utils/roundNumber.js +++ b/platform/core/src/utils/roundNumber.js @@ -10,24 +10,33 @@ * @param value - to return a fixed measurement value from * @param precision - defining how many digits after 1..9 are desired */ -function roundNumber(value: string | number, precision = 2): string { - if (value === undefined || value === null || value === '') return 'NaN'; + +function roundNumber(value, precision = 2) { + if (Array.isArray(value)) { + return value.map((v) => roundNumber(v, precision)).join(", "); + } + if (value === undefined || value === null || value === "") { + return "NaN"; + } value = Number(value); - if (value < 0.0001) return `${value}`; + const absValue = Math.abs(value); + if (absValue < 0.0001) { + return `${value}`; + } const fixedPrecision = - value >= 100 + absValue >= 100 ? precision - 2 - : value >= 10 - ? precision - 1 - : value >= 1 - ? precision - : value >= 0.1 - ? precision + 1 - : value >= 0.01 - ? precision + 2 - : value >= 0.001 - ? precision + 3 - : precision + 4; + : absValue >= 10 + ? precision - 1 + : absValue >= 1 + ? precision + : absValue >= 0.1 + ? precision + 1 + : absValue >= 0.01 + ? precision + 2 + : absValue >= 0.001 + ? precision + 3 + : precision + 4; return value.toFixed(fixedPrecision); }