Value is not shown as it should be

0

Good, I have a code which is giving me a problem.

My code is made up of 3 things, a total variable, another for money received and another to show change or pending payment (depending on the result I received when verifying the total and the money received), for example if my total es = 500, my received = 400 m and another variable must be = Failure to pay: 100.

So far I have a working code but the detail is to show this last variable, it shows me the correct result but I can not make it show if it is change or if it is money pending payment, also the result it gives me is a number Negative whole, I would like it to be shown without the negative if it were a change.

Here is an example

$(document).ready(function() {
  
  var dolar = 17.50;
  
  $('#Total').on('keyup', function() {
    var valor = $(this).val();
    
    if (valor == "") {
      $('#TotalLbl').text("$00.00");
    } else {
      $('#TotalLbl').text("Total: $" + parseFloat(valor).toFixed(2));
    }
  });
  $('.money').on('keyup', function() {
    var dollar = $('#USD').val() * dolar;
    var mx = $('#MXN').val();
    valor = parseFloat(mx) + parseFloat(dollar);
    
    if (valor == "") {
      $('#Received').text("$00.00");
    } else {
      $('#Received').text("Received: $" + parseFloat(valor).toFixed(2));
      var falta = ($('#Total').val() - valor);
      var tot = $('#Total').val(); 
      console.log(falta);
      if (parseFloat(falta) < parseFloat(tot))
        $('#INeedThisTo0').text("Cambio: $" + parseFloat(falta).toFixed(2));
      else
        $('#INeedThisTo0').text("Pendiente: $" + parseFloat(falta).toFixed(2));
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input placeholder="Total.." type="number" id="Total" />

<input placeholder="MXN.." type="number" class="money" id="MXN" />

<input placeholder="USD.." type="number" class="money" id="USD" />

<br>

<label id="TotalLbl"></label>

<label id="Received"></label>

<label id="INeedThisTo0"></label>

<label id="Change"></label>
    
asked by Francisco Fernandez 25.04.2017 в 15:54
source

2 answers

1

The problem is that according to the logic you have implemented, checking the if will always be true , so the statement declared in the else is never executed. A possible solution could be to check if the missing value is greater or less than 0.

Something like this:

if (parseFloat(falta) < 0){
    $('#INeedThisTo0').text("Cambio: $" + parseFloat(falta).toFixed(2));
  } else {
    $('#INeedThisTo0').text("Pendiente: $" + parseFloat(falta).toFixed(2));
  }

And to show a positive value, simply multiply by (-1):

if (parseFloat(falta) < 0){
    $('#INeedThisTo0').text("Cambio: $" + parseFloat(falta).toFixed(2));
  } else {
    $('#INeedThisTo0').text("Pendiente: $" + (parseFloat(falta).toFixed(2) * (-1)));
  }
    
answered by 25.04.2017 / 16:16
source
0

To make a negative value positive:

 $('#INeedThisTo0').text("Cambio: $" + parseFloat(falta * (-1)).toFixed(2));

Greetings

    
answered by 25.04.2017 в 16:14