diff --git a/myproject/pos/static/pos/js/terminal.js b/myproject/pos/static/pos/js/terminal.js index c7c01c3..a8aa483 100644 --- a/myproject/pos/static/pos/js/terminal.js +++ b/myproject/pos/static/pos/js/terminal.js @@ -1,5 +1,17 @@ // POS Terminal JavaScript +/** + * Округляет число до N знаков после запятой для корректного отображения. + * Решает проблему погрешности float arithmetic в JavaScript. + * @param {number} value - Число для округления + * @param {number} decimals - Количество знаков после запятой (по умолчанию 3) + * @returns {number} Округлённое число + */ +function roundQuantity(value, decimals = 3) { + if (value === null || value === undefined || isNaN(value)) return 0; + return Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals); +} + const CATEGORIES = JSON.parse(document.getElementById('categoriesData').textContent); let ITEMS = []; // Будем загружать через API let showcaseKits = JSON.parse(document.getElementById('showcaseKitsData').textContent); @@ -595,10 +607,11 @@ function renderProducts() { const inCart = cart.has(cartKey) ? cart.get(cartKey).qty : 0; const free = available - reserved - inCart; + const freeRounded = roundQuantity(free, 3); // Округляем для отображения // Создаём элементы для стилизации разных размеров const freeSpan = document.createElement('span'); - freeSpan.textContent = free; + freeSpan.textContent = freeRounded; // Используем округлённое значение freeSpan.style.fontSize = '1.1em'; freeSpan.style.fontWeight = 'bold'; freeSpan.style.fontStyle = 'normal'; @@ -606,10 +619,10 @@ function renderProducts() { // Отображаем резерв и корзину если они есть const suffixParts = []; if (reserved > 0) { - suffixParts.push(`−${reserved}`); + suffixParts.push(`−${roundQuantity(reserved, 3)}`); } if (inCart > 0) { - suffixParts.push(`−${inCart}🛒`); + suffixParts.push(`−${roundQuantity(inCart, 3)}🛒`); } if (suffixParts.length > 0) {