From 9e1bae5911b705283fc87e93a233d79b3c0a093a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 18 Mar 2020 11:57:54 +0100 Subject: [PATCH] improvements in decimals --- CockpitSpace/Speedometer.js | 4 ++-- Utilities.js | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CockpitSpace/Speedometer.js b/CockpitSpace/Speedometer.js index a1c7c35aa..12f8cc14e 100644 --- a/CockpitSpace/Speedometer.js +++ b/CockpitSpace/Speedometer.js @@ -198,8 +198,8 @@ function newSpeedometer () { if (isNaN(label) === false) { label = Number(label) - label = Math.trunc(label * Math.pow(10, thisObject.params.DECIMALS)) / Math.pow(10, thisObject.params.DECIMALS) - if (label === 0) { label = label.toFixed(2) } else { label = label.toLocaleString() } + label = dynamicDecimals(label, 2) + if (label === 0) { label = label.toFixed(2) } } // label = label.substring(0, 6) diff --git a/Utilities.js b/Utilities.js index c29e0a4f1..48750b49b 100644 --- a/Utilities.js +++ b/Utilities.js @@ -22,7 +22,10 @@ function drawContainerBackground (container, color, opacity, fitFunction) { browserCanvasContext.fill() } -function dynamicDecimals (value) { +function dynamicDecimals (value, minDecimals) { + if (minDecimals === undefined) { + minDecimals = 0 + } let decimals = 2 if (value < 1) { decimals = 4 } if (Math.abs(Math.trunc(value * 100)) < 1) { decimals = 6 } @@ -30,7 +33,7 @@ function dynamicDecimals (value) { if (Math.abs(Math.trunc(value * 1000000)) < 1) { decimals = 10 } if (Math.abs(Math.trunc(value * 100000000)) < 1) { decimals = 12 } if (Math.abs(Math.trunc(value * 10000000000)) < 1) { value = 0; decimals = 0 } - if (Math.trunc(value) === value) { decimals = 0 } + if (Math.trunc(value) === value) { decimals = minDecimals } let returnValue = Number(value).toFixed(decimals) return returnValue }