From 88a47fbd1abc02842fcef9242301dd6260d9b484 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Sun, 2 Mar 2025 13:32:33 -0800 Subject: [PATCH] Formular rounding fixes --- ehr/resources/web/ehr/DataEntryUtils.js | 2 +- ehr/resources/web/ehr/utils.js | 16 +++------------- ehr/resources/web/ehr/window/DrugAmountWindow.js | 3 ++- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/ehr/resources/web/ehr/DataEntryUtils.js b/ehr/resources/web/ehr/DataEntryUtils.js index 7833a71af..b3219d0f7 100644 --- a/ehr/resources/web/ehr/DataEntryUtils.js +++ b/ehr/resources/web/ehr/DataEntryUtils.js @@ -874,7 +874,7 @@ EHR.DataEntryUtils = new function(){ } } - if (!amount && Ext4.isEmpty(rounding)){ + if (amount && !Ext4.isEmpty(rounding)){ amount = EHR.Utils.roundToNearest(amount, rounding); } diff --git a/ehr/resources/web/ehr/utils.js b/ehr/resources/web/ehr/utils.js index aa0c3926b..580e6909e 100644 --- a/ehr/resources/web/ehr/utils.js +++ b/ehr/resources/web/ehr/utils.js @@ -497,19 +497,9 @@ EHR.Utils = new function(){ return val; } - var remainder = val % round; - var remainderPct = remainder / round; - var base = Math.floor(val / round); - if (remainder === 0){ - return val; - } - //note: JS seems to handle division poorly in situations like 4 / 0.1 - else if (remainderPct < 0.5 || remainderPct > 0.9999){ - return (base * round); - } - else { - return (base * round) + round; - } + let rounded = Math.round(val / round) * round; + let precision = (round.toString().split('.')[1] || "").length; // Get decimal places in `round` + return rounded.toFixed(precision); // toFixed handles floating point precision issues }, editUIButtonCore: function(schemaName, queryName, dataRegionName, paramMap, copyFilters, params){ diff --git a/ehr/resources/web/ehr/window/DrugAmountWindow.js b/ehr/resources/web/ehr/window/DrugAmountWindow.js index 852ceb509..1fa08fb93 100644 --- a/ehr/resources/web/ehr/window/DrugAmountWindow.js +++ b/ehr/resources/web/ehr/window/DrugAmountWindow.js @@ -394,7 +394,8 @@ Ext4.define('EHR.window.DrugAmountWindow', { found = true; editor = { xtype: 'ldk-numberfield', - fieldName: fieldName + fieldName: fieldName, + decimalPrecision: 6 } }