From 7156d53edadf70d4b7e5d31458f0dc1b856f5099 Mon Sep 17 00:00:00 2001
From: Joris
Date: Sat, 9 Jul 2022 23:22:15 +0200
Subject: Fix number parsing and format
---
public/main.js | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
(limited to 'public')
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 = ``
+ const value = parseNumber(number.innerHTML)
+ number.innerHTML = ``
// 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
}
--
cgit v1.2.3