1

Como diminuir os números decimais dessa tabuada de divisão ? tava pensando em colocar um tofixed(), mas estou com dificuldade de achar uma sintaxe correta pra encaixar nesse código. como posso resolver isso, alguém pode ajudar?

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

0

2 Respostas 2

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

Se entendi bem o que você quer fazer é isso não?

2
  • sim isso ai, mas não to sabendo como encaixar ele no meu codigo, sempre que eu tento colocar ele da algum erro, da uma olhada no meu codigo dei uma editada nele agora da pra entender melhor como ele ta 16/04/2020 às 2:45
  • Tentar colocar assim: item4.text = `${n} / ${c} = ${(n/c).toFixed(2)}`; 16/04/2020 às 2:47
1

Dentro última da expressão(resultado) embutida na string:

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

Coloque um parênteses () em torno de n/c e nesse valor evoque o método toFixed():

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

No seu código:

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>

1
  • Agora deu certo, eu tava quebrando cabeça com isso, sou novo na programação, obrigado ! 16/04/2020 às 4:06

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .