aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris2021-05-03 21:15:54 +0200
committerJoris2021-05-03 21:15:54 +0200
commitad2c99ba0c1e6772860226c4459849f9f1dcfdc4 (patch)
tree6cf8ea21cf01288d5adc36f84414ef590182e73b
parent978a9c45d1d36038adfb8bcc7e9a23c84fb10603 (diff)
Fix number inputs
-rw-r--r--public/main.js27
1 files changed, 20 insertions, 7 deletions
diff --git a/public/main.js b/public/main.js
index 68deeee..24d2f8d 100644
--- a/public/main.js
+++ b/public/main.js
@@ -6,7 +6,7 @@ window.onload = function() {
document.querySelectorAll('.number').forEach(function (number) {
// Install input
- const value = parseInt(number.innerHTML)
+ const value = parseFloat(number.innerHTML)
number.innerHTML = `<input value="${value}">`
// Push to inputs
@@ -28,7 +28,7 @@ window.onload = function() {
// Apply factor to other inputs
inputs.forEach(function (input) {
if (input.element !== currentInput.element) {
- input.element.value = formatNumber(factor * input.value)
+ input.element.value = formatNumber(factor, input.value)
}
})
}
@@ -45,11 +45,24 @@ window.onload = function() {
}
-function formatNumber(n) {
- const xs = n.toString().split('.')
- if (xs.length == 2) {
- return `${xs[0]}.${xs[1].slice(0, 1)}`
+function formatNumber(factor, value) {
+ if (factor === 1) {
+ return value
} else {
- return n
+ 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)}`
+ } else {
+ return n
+ }
+ }
+}
+
+function precision(value) {
+ const xs = value.toString().split('.')
+ if (xs.length === 2) {
+ return xs[1].length
}
}