diff options
author | Joris | 2022-07-09 23:22:15 +0200 |
---|---|---|
committer | Joris | 2022-07-09 23:22:15 +0200 |
commit | 7156d53edadf70d4b7e5d31458f0dc1b856f5099 (patch) | |
tree | c153136419c7a08dbe00eb09517568fed23fe307 | |
parent | 87152a837819dd3ede0c8d8906a7543b183decfa (diff) |
Fix number parsing and format
-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 } |