Error comparing two strings with float value

0

Good afternoon!

I am trying to compare numbers with Jquery, they are values with decimals or not. when the value goes up to 999 everything works perfectly, but above that it does not work as it should.

Example: value_mo_contract = 510.00

total_mobra_orc_calc = 1000

$(document).ready(function(){
var valor_mo_contrato_s = parseFloat($("#valor_mo_contrato").val()).toFixed(2);
var valor_mobra_s = parseFloat($("#total_mobra_orc_calc").val()).toFixed(2);

  if (valor_mo_contrato_s < valor_mobra_s) {
     $("#valor_unitario_orc_check").text("O Sistema sugere que o Fiscal verifique o valor de Mao de Obra.");
    $("#valor_unitario_orc_check").css({"color": "red"});
  }
});
    
asked by anonymous 20.03.2017 / 18:07

1 answer

3

You are comparing 2 strings using the < because you are using .toFixed(2) method after running parseFloat

You would need to compare the two values directly as Number and not as string .

One of the possible solutions is to convert again with parseFloat when comparing in if .

$(document).ready(function(){
var valor_mo_contrato_s = parseFloat($("#valor_mo_contrato").val()).toFixed(2);
var valor_mobra_s = parseFloat($("#total_mobra_orc_calc").val()).toFixed(2);

  if (parseFloat(valor_mo_contrato_s) < parseFloat(valor_mobra_s)) {
     $("#valor_unitario_orc_check").text("O Sistema sugere que o Fiscal verifique o valor de Mao de Obra.");
    $("#valor_unitario_orc_check").css({"color": "red"});
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" id="valor_mo_contrato" value="500.3">
<input type="text" id="total_mobra_orc_calc" value="1000">
<div id="valor_unitario_orc_check"></div>
    
20.03.2017 / 18:45