diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-07-11 14:24:39 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-07-11 14:24:39 +0200 |
commit | 7bffa67a7fe434e6d163e50bbf86f57fa0ee5b66 (patch) | |
tree | 8d210e75bafed8eb6ca74813a84455eb4ca714a0 | |
parent | 6683cfdf3cbb6ec1b411e76f91182d5de4477daf (diff) | |
download | tres-7bffa67a7fe434e6d163e50bbf86f57fa0ee5b66.tar.gz tres-7bffa67a7fe434e6d163e50bbf86f57fa0ee5b66.tar.bz2 tres-7bffa67a7fe434e6d163e50bbf86f57fa0ee5b66.zip |
feat: Add support for floating numbers
Add support for floating points using decimal comma format.
-rw-r--r-- | index.html | 32 |
1 files changed, 21 insertions, 11 deletions
@@ -97,14 +97,14 @@ main { </header> <main> <div class="section"> - <input class="form-ctl" type="number" id="a" placeholder="A"> + <input class="form-ctl" pattern="([0-9]+.{0,1}[0-9]*,{0,1})*[0-9]" type="text" id="a" placeholder="A"> = - <input class="form-ctl" type="number" id="b" placeholder="B"> + <input class="form-ctl" pattern="([0-9]+.{0,1}[0-9]*,{0,1})*[0-9]"type="text" id="b" placeholder="B"> </div> <div class="section"> - <input class="form-ctl" type="number" id="c" placeholder="C"> + <input class="form-ctl" pattern="([0-9]+.{0,1}[0-9]*,{0,1})*[0-9]" type="text" id="c" placeholder="C"> = - <input class="form-ctl" type="number" id="resultado" disabled placeholder="Resultado"> + <input class="form-ctl" pattern="([0-9]+.{0,1}[0-9]*,{0,1})*[0-9]" id="resultado" disabled placeholder="Resultado"> </div> <div id="warning" class="section warning"> <p> @@ -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" } |