From eda2b1406460390a2fbe62f4e4f4b9d6fef6c22f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 21 Aug 2020 17:33:27 +0200 Subject: [PATCH] Decimals Handling Improved --- FloatingSpace/UiObject.js | 6 ++++-- Plotting/RecordValues.js | 8 ++++---- Utilities.js | 15 ++++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/FloatingSpace/UiObject.js b/FloatingSpace/UiObject.js index 458101cc6..fb77577ce 100644 --- a/FloatingSpace/UiObject.js +++ b/FloatingSpace/UiObject.js @@ -110,6 +110,7 @@ function newUiObject() { let errorMessage = '' let currentValue = 0 + let valueMinDecimals = undefined let currentPercentage = '' let currentStatus = '' let rightDragging = false @@ -745,10 +746,11 @@ function newUiObject() { hasError = false } - function setValue(value, counter) { + function setValue(value, counter, minDecimals) { if (value !== undefined) { currentValue = value hasValue = true + valueMinDecimals = minDecimals if (counter !== undefined) { valueCounter = counter } else { @@ -1438,7 +1440,7 @@ function newUiObject() { label = currentValue if (!isNaN(currentValue)) { if (currentValue.toFixed !== undefined) { - label = dynamicDecimals(currentValue) + label = dynamicDecimals(currentValue, valueMinDecimals) } } diff --git a/Plotting/RecordValues.js b/Plotting/RecordValues.js index 931e988a8..799b3d4a8 100644 --- a/Plotting/RecordValues.js +++ b/Plotting/RecordValues.js @@ -95,14 +95,14 @@ function newRecordValues() { pointing to the same property at the Target Node. */ if (property.config.childProperty !== undefined) { - applyValue(targetNode[property.config.childProperty], value) + applyValue(targetNode[property.config.childProperty], value, property.config.decimals) } else { - applyValue(targetNode[property.config.codeName], value) + applyValue(targetNode[property.config.codeName], value, property.config.decimals) } } } - function applyValue(node, value) { + function applyValue(node, value, minDecimals) { if (canvas.chartingSpace.visible !== true) { return } if (node === undefined) { return } if (node === undefined) { return } @@ -110,6 +110,6 @@ function newRecordValues() { if (node.payload.uiObject === undefined) { return } if (value === true) { value = 'true' } if (value === false) { value = 'false' } - node.payload.uiObject.setValue(value, 1) + node.payload.uiObject.setValue(value, 1, minDecimals) } } diff --git a/Utilities.js b/Utilities.js index 11eeb5a37..5c68b4b3a 100644 --- a/Utilities.js +++ b/Utilities.js @@ -7,14 +7,15 @@ 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 } - if (Math.abs(Math.trunc(value * 10000)) < 1) { decimals = 8 } - if (Math.abs(Math.trunc(value * 1000000)) < 1) { decimals = 10 } - if (Math.abs(Math.trunc(value * 100000000)) < 1) { decimals = 12 } + let decimals = 0 + if (value < 1) { decimals = 2 } + if (Math.abs(Math.trunc(value * 100)) < 1) { decimals = 4 } + if (Math.abs(Math.trunc(value * 10000)) < 1) { decimals = 6 } + if (Math.abs(Math.trunc(value * 1000000)) < 1) { decimals = 8 } + if (Math.abs(Math.trunc(value * 100000000)) < 1) { decimals = 10 } if (Math.abs(Math.trunc(value * 10000000000)) < 1) { value = 0; decimals = 0 } - if (Math.trunc(value) === value) { decimals = minDecimals } + if (decimals < minDecimals) {decimals = minDecimals} + let returnValue = Number(value).toFixed(decimals) return returnValue }