'rounding numbers: Metric-imperial measurements conversion calculator / scrimba project
I'm pretty much finished with a conversion calculator project from scrimba, but have had no success with rounding number to 3 decimals.
I have played around with math.round/math.floor with no success.
Any suggestions would be appreciated!
Here is a snippet of my work:
let feetOutput = document.getElementById("feet-output");
let meterOutput = document.getElementById("meter-output");
meterInput = document.getElementById("meter-input");
let mainInput = document.getElementById("main-input");
litterInput = document.getElementById("litter-input")
feetInput = document.getElementById("feet-input");
document.getElementById("main-Input").value = '';
document.getElementById("main-Input").textContent = 0;
function lengthConverter(valNum) {
document.getElementById("feet-output").innerHTML = valNum * 3.281 + " Feet";
document.getElementById("meter-output").innerHTML = valNum / 3.281 + " meters";
document.getElementById("gallons-output").innerHTML = valNum / 3.785 + " gallons";
document.getElementById("litter-output").innerHTML = valNum * 3.785 + " liters";
document.getElementById("pounds-output").innerHTML = valNum * 2.205 +
" pounds";
document.getElementById("kg-output").innerHTML = valNum / 2.205 + " kilograms";
}
function lengthConvertermf(valNum) {
document.getElementById("feet-output").innerHTML = roundedUp((((valNum * 10) * (3.281 * 10)) / 10) * 1000 / 1000 + " Feet");
}
function lengthConverterfm(valNum) {
document.getElementById("meter-output").innerHTML = (((((valNum * 10) / (3.281 * 10) / 10) * 1000) / 1000) + " meters");
Math.round((num + Number.EPSILON) * 100) / 100
}
function lengthConverterlg(valNum) {
document.getElementById("gallons-output").innerHTML = roundedUp(valNum / 3.785 + " gallons");
}
function lengthConvertergl(valNum) {
document.getElementById("litter-output").innerHTML = roundedUp(valNum * 3.785 + " liters");
}
function lengthConverterkp(valNum) {
document.getElementById("pounds-output").innerHTML = roundedUp(valNum * 2.205) +
" pounds";
}
function lengthConverterpk(valNum) {
document.getElementById("kg-output").innerHTML = roundedUp(valNum / 2.205 + " kilograms");
}
Solution 1:[1]
const round = (num, n) => Number.parseFloat(num.toFixed(n));
//using:
console.log(round(3.14159, 3));
const floor = (num, n) => Number.parseFloat(num.toFixed(n)) - Math.pow(10, -n);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
