From 7bffa67a7fe434e6d163e50bbf86f57fa0ee5b66 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Mon, 11 Jul 2022 14:24:39 +0200 Subject: feat: Add support for floating numbers Add support for floating points using decimal comma format. --- index.html | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 39697ff..65ee3f8 100644 --- a/index.html +++ b/index.html @@ -97,14 +97,14 @@ main {
- +  =  - +
- +  =  - +

@@ -122,14 +122,24 @@ const cInput = document.getElementById('c') const resultadoInput = document.getElementById('resultado') const warningParagraph = document.getElementById('warning') +function cToP(v) { + return v.replace(",", ".") +} + +function pToC(v) { + return v.replace(".", ",") +} + function eval() { - const a = aInput.value - const b = bInput.value - const c = cInput.value + const a = cToP(aInput.value) + const b = cToP(bInput.value) + const c = cToP(cInput.value) try { const values = validate(a,b,c) - resultadoInput.value = calculate(...values) + // fix float to 3 and remove zeroes + const result = calculate(...values).toFixed(3).replace(/\.0+$/,'') + resultadoInput.value = pToC(result) } catch (e) { warningParagraph.textContent = e warningParagraph.style.visibility = 'visible'; @@ -142,19 +152,19 @@ function eval() { function validate(a, b, c) { const result = [0,0,0] - const ra = parseInt(a) + const ra = parseFloat(a) if (isNaN(ra)){ throw "Valor de A invalido" } result[0] = ra - const rb = parseInt(b) + const rb = parseFloat(b) if (isNaN(rb)){ throw "Valor de B invalido" } result[1] = rb - const rc = parseInt(c) + const rc = parseFloat(c) if (isNaN(rc)){ throw "Valor de C invalido" } -- cgit v1.2.3