How to decrease decimal numbers

Asked

Viewed 56 times

1

How to decrease the decimal numbers of this division table ? I was thinking of putting a tofixed(), but I’m having a hard time finding a correct syntax to fit this code. how can I fix this, someone can help?

function tabuada() {
    let num = document.getElementById('txtn')
    let tab4 = document.getElementById('seltab4')
    if (num.value.length == 0) {
        window.alert('digite um numero!')
    } else {
        let n = Number(num.value) 
        let c = 1 
        tab4.innerHTML = ''
        while (c <= 10) { 
            let item4 = document.createElement('option') 
            item4.text = `${n} / ${c} = ${n/c}` 
            item4.value = `tab4${c}` 
            tab4.appendChild(item4) 
            c++ 
        }
    }
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="estilo.css">
</head>
<body>
    <header>
        <h1>Tabuada</h1>
</header>
<section>
    <div>
        <p>Numero: <input type="number" name="num" id="txtn">
        <input type="button" value="Gerar tabuada" onclick="tabuada()"></p>
    </div>
    
    <div>
        <select name="Tabuada4" id="seltab4" size="10" ></select>
    </div>

</section> 

<script src="aula06.js"></script>  
</body>
</html>

Tabuada

2 answers

3

var x = 9.656;
x.toFixed(0);           // returns 10
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000

If I understand correctly what you want to do is this no?

  • yes that there, but not knowing how to fit it in my code, whenever I try to put it of some error, give a look at my code I gave an edited in it now da para entender melhor como ta

  • Try to put it like this: Item4.text = `${n} / ${c} = ${(n/c). toFixed(2)}`;

1


Inside last of the string embedded expression(result):

item4.text = ${n} / ${c} = ${n/c}

Enclose () around n/c and at that value evoke the method toFixed():

item4.text = ${n} / ${c} = ${(n/c).toFixed(2)}

In your code:

function tabuada() {
    let num = document.getElementById('txtn')
    let tab4 = document.getElementById('seltab4')
    if (num.value.length == 0) {
        window.alert('digite um numero!')
    } else {
        let n = Number(num.value) 
        let c = 1 
        tab4.innerHTML = ''
        while (c <= 10) { 
            let item4 = document.createElement('option') 
            item4.text = `${n} / ${c} = ${(n/c).toFixed(2)}` 
            item4.value = `tab4${c}` 
            tab4.appendChild(item4) 
            c++ 
        }
    }
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="estilo.css">
</head>
<body>
    <header>
        <h1>Tabuada</h1>
</header>
<section>
    <div>
        <p>Numero: <input type="number" name="num" id="txtn">
        <input type="button" value="Gerar tabuada" onclick="tabuada()"></p>
    </div>
    
    <div>
        <select name="Tabuada4" id="seltab4" size="10" ></select>
    </div>

</section> 

<script src="aula06.js"></script>  
</body>
</html>

  • Now it worked, I was breaking my head with this, I’m new in programming, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.