How can I do forever that put decimals on my calculator they have a point ? the language is Javascript!

Asked

Viewed 48 times

-1

Next friends, I am creating a calculator for the project of the facul and already I did 95% just need to be able to let it automates whenever I type numbers as for example: 1000 would have to leave 1,000 and 10000 would have to leave 10,000 etc... Could you help me?

HTML code:

<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculadora</title>
<link rel="stylesheet" href="css/calculadora.css">
<!-- Adicione aqui seus scripts para testar -->
</head>
<body>
    <div class="calculadora">
    <input type="text" id="resultado" disabled /><br>

    <button onclick="pegarNum(this)">1</button>
    <button onclick="pegarNum(this)">2</button>
    <button onclick="pegarNum(this)">3</button>
    <button onclick="operador(this)">+</button><br>

    <button onclick="pegarNum(this)">4</button>
    <button onclick="pegarNum(this)">5</button>
    <button onclick="pegarNum(this)">6</button>
    <button onclick="operador(this)">-</button><br>

    <button onclick="pegarNum(this)">7</button>
    <button onclick="pegarNum(this)">8</button>
    <button onclick="pegarNum(this)">9</button>
    <button onclick="operador(this)">*</button><br>

    <button onclick="pegarNum(this)">.</button>
    <button onclick="pegarNum(this)">0</button>
    <button onclick="limparTudo()">C</button>
    <button onclick="operador(this)">/</button><br>
    <button onclick="igual()">=</button> 

    </div>


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

The Javascript code is this:

let numero = "";
let telaCalculadora = "";
let termo = [undefined,undefined,undefined];
let resultado = "";



function pegarNum(num){   
    numero = numero.concat(num.innerHTML);                         
    mostrarNaTela(num.innerHTML);    
}


function limparMemoria(){
    numero = "";
    termo = [undefined,undefined,undefined];
    resultado = "";
}

function mostrarNaTela(conteudo){
    telaCalculadora = telaCalculadora.concat(conteudo);
    let tela = document.getElementById('resultado');
    tela.value = telaCalculadora;
}

function limparTela(){
    telaCalculadora = "";
    let tela = document.getElementById('resultado');
    tela.value = telaCalculadora;
}

function limparTudo(){
    limparMemoria();
    limparTela();
}



function operador(op){

    if (numero != ""){
        if(termo[1] == undefined){
            termo[0] = numero;
            termo[1] = op.innerHTML;
            mostrarNaTela(op.innerHTML);
            numero = ""; 
        }
      }
    }

function igual(){
    if(termo[0] != undefined && termo[1] != undefined && numero != ""){

        termo[2] = numero;

        switch(termo[1]){
            case '+':
                resultado = Number(termo[0]) + Number(termo[2]);
            break;

            case '-':
                resultado = Number(termo[0]) - Number(termo[2]);
            break;

            case '*':
                resultado = Number(termo[0]) * Number(termo[2]);
            break;

            case '/':
                resultado = Number(termo[0]) / Number(termo[2]);
            break;
        }

        let salvarResultado = resultado;
        limparTela();
        mostrarNaTela(resultado)
        limparMemoria();
        numero = salvarResultado.toString();

    }else if(termo[0] != undefined && termo[1] != undefined && numero == ""){
        let erro = "Erro de operação";
        limparTela();
        mostrarNaTela(erro);        
        limparMemoria();        
    }


}

  • 1

    "put decimals on my calculator" wouldn’t be thousands? doesn’t need the "the language is Javascript!" in the title, already on the tag ;)

  • 1

    In the duplicate suggested above, the accepted answer suggests using toLocaleString, that in addition to being native to the language, is much better than the answer below, which is pure gambiarra (the fact that you need to double check if you have a point at the beginning for sure is the "high point"). I know that the author of the question is free to accept the answer, but I thought it good to leave the warning here: there is already a native option in the language and I would only use a function of my own if I were to do something that the native function does not offer (which is clearly not the case here) :-)

1 answer

-2


Add the Makedecimal function to your file script js. and in function equal() in each marry you call the function , so:

resultado = MakeDecimal(Number(termo[0]) + Number(termo[2]));

//Function to transform number into decimal

function MakeDecimal(Number) {
  Number = Number + "" // Convert Number to string if not
  Number = Number.split('').reverse().join(''); //Reverse string
  var Result = "";
   for (i = 0; i <= Number.length; i += 3) {
     Result = Result + Number.substring(i, i + 3) + ".";
   }
     Result = Result.split('').reverse().join(''); //Reverse again
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, 
     Result.length); // Remove first dot, if have.
        if (!isFinite(Result.substring(0, 1))) Result = Result.substring(1, 
     Result.length); // Remove first dot, if have.
        return Result;
 }

Browser other questions tagged

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