diff options
-rw-r--r-- | public/main.js | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/public/main.js b/public/main.js index 24d2f8d..1c0914a 100644 --- a/public/main.js +++ b/public/main.js @@ -6,8 +6,8 @@ window.onload = function() { document.querySelectorAll('.number').forEach(function (number) { // Install input - const value = parseFloat(number.innerHTML) - number.innerHTML = `<input value="${value}">` + const value = parseNumber(number.innerHTML) + number.innerHTML = `<input value="${formatNumber(1, value)}">` // Push to inputs const element = number.children[0] @@ -16,7 +16,7 @@ window.onload = function() { element.addEventListener('input', function() { // Parse modified input value - const n = parseFloat(element.value.replace(',', '.')) || 0 + const n = parseNumber(element.value) if (!isNaN(n)) { // Find current factor @@ -45,15 +45,19 @@ window.onload = function() { } +function parseNumber(value) { + return parseFloat(value.replace(',', '.')) || 0 +} + function formatNumber(factor, value) { if (factor === 1) { - return value + return value.toString().split('.').join(',') } else { const n = factor * value const xs = n.toString().split('.') const p = precision(value) || 1 if (xs.length == 2) { - return `${xs[0]}.${xs[1].slice(0, p)}` + return `${xs[0]},${xs[1].slice(0, p)}` } else { return n } |