Subtraction with decimal places in the result

Asked

Viewed 139 times

0

  1. I have a problem that I can not solve, Function calculate() subtracts the price by the total paid and shows in the amount, only when showing in quantiafalta, it shows so:

Price: 62.000 Total Paid: 60.000 Outstanding amount: 2

And I wanted him to show it like this:

Price: 62.000 Total Paid: 60.000 Missing Amount: 2,000

<script>
function calcular() {
    var num1 = Number(document.getElementById("Preco").value);
    var num2 = Number(document.getElementById("TotalPago").value);
    var elemResult = document.getElementById("QuantiaFalta")
    if (elemResult.textContent === undefined) {
       elemResult.textContent = String(num1 - num2);
    }
    else {
       elemResult.innerText = String(num1 - num2);
    }
}
</script>
<div class="form-group">
                    <label class="col-sm-3 control-label">Pre&ccedilo</label>
                    <div class="col-sm-5">
                        <input type="text" name="fPreco" id="Preco" class="w3-input w3-animate-input" style="width:135px; border-bottom: 2px solid black" onblur="calcular();" value="<?php echo $Preco; ?>" readonly="readonly">
                    </div>
                </div>
<div class="form-group">
                    <label class="col-sm-3 control-label">Total Pago *</label>
                    <div class="col-sm-5">
                        <input type="text" name="fTotalPago" id="TotalPago" class="w3-input w3-animate-input" style="width:350px; border-bottom: 2px solid orange" onblur="calcular();" value="<?php echo $Preco; ?>" required>
                    </div>
                </div>
<div class="form-group">
                    <label class="col-sm-3 control-label">Quantia em Falta *</label>
                    <div class="col-sm-5">
                        <span type="text" name="fQuantiaFalta" id="QuantiaFalta" class="w3-input" style="width:350px; border-bottom: 2px solid orange" required></span>
                    </div>
                </div>
  • When you are creating the question, you are asked to do a search here in the community to see if this problem has not been solved and so send duplicate content. What have you researched? I also invite you to do the [tour] to learn the basics of how the site works.

  • 1

    62,000 is 62,000 or 62 with 3 decimal places? The problem is that at this point if it is written 62,000 parseint returns 62

  • 1
  • 1

    a simple way and set input inputs as 62000.00 and 60000.00

3 answers

1

The correct is to check the amounts of decimals for a proper return. This check is done on the line var quantDecimais

With the 62.00 input Preço and 60.00 in input TotalPago the result will be presented correctly, as well as 6200.00 and 6000.00 will also return a coherent result.

function calcular() {

    var num1 = (document.getElementById("Preco").value);
    
    var quantDecimais = (num1 + "").split(".")[1].length;
    
    console.log (quantDecimais);       
    
    var num2 = (document.getElementById("TotalPago").value);
    var elemResult = document.getElementById("QuantiaFalta")

    if (elemResult.textContent === undefined) {
       elemResult.textContent = (num1 - num2).toFixed(quantDecimais);
    }
    else {
       elemResult.innerText = (num1 - num2).toFixed(quantDecimais);
    }
}
<div class="form-group">
                    <label class="col-sm-3 control-label">Pre&ccedilo</label>
                    <div class="col-sm-5">
                        <input type="text" name="fPreco" id="Preco" class="w3-input w3-animate-input" style="width:135px; border-bottom: 2px solid black" onblur="calcular();" value="">
                    </div>
                </div>
<div class="form-group">
                    <label class="col-sm-3 control-label">Total Pago *</label>
                    <div class="col-sm-5">
                        <input type="text" name="fTotalPago" id="TotalPago" class="w3-input w3-animate-input" style="width:350px; border-bottom: 2px solid orange" onblur="calcular();" value="" required>
                    </div>
                </div>
<div class="form-group">
                    <label class="col-sm-3 control-label">Quantia em Falta *</label>
                    <div class="col-sm-5">
                        <span type="text" name="fQuantiaFalta" id="QuantiaFalta" class="w3-input" style="width:350px; border-bottom: 2px solid orange"></span>
                    </div>
                </div>

0

You can use toFixed to determine how many decimal places the query will return. For example:

function calcular() {
    var num1 = 62.000;
    var num2 = 60.000;
    var elemResult = num1-num2;
    document.write(elemResult.toFixed(3));
}
calcular();

In this case its value will return a value with 3 decimal places.

  • 1

    Recalling that 62,000 for Javascript is sixty-two, while everything indicates that it should be sixty-two thousand. That is, it is necessary to format the initial value properly also.

  • If the entry is 62.00 with 60.00 will give 2.000

0


The solution would be to add toFixed at the end of the result

function calcular() {
    var num1 = Number(document.getElementById("Preco").value);
    var num2 = Number(document.getElementById("TotalPago").value);
    var elemResult = document.getElementById("QuantiaFalta")
    var fixa = 3;
    if (elemResult.textContent === undefined) {
       elemResult.textContent = (num1 - num2).toFixed(fixa);
    }
    else {
       elemResult.innerText = (num1 - num2).toFixed(fixa);
    }
}

Browser other questions tagged

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