improvements in decimals

This commit is contained in:
Luis Fernando Molina committed 2020-03-18 11:57:54 +01:00
1 parent 5ef91bbe40
commit 9e1bae5911
2 files changed
+7 -4

No files matched your search

+2 -2
View File
@@ -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)
+5 -2
View File
@@ -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
}