JavaScript calculation

2

I have two input fields where I type values. I put a mask so that when I enter the value it will be in 0.00 format. How do I make a simple calculation with 0.00? Because I have a formula that I have passed and that the result, or the 0 or appears NaN. Follow the formula code of the calculation.

$(function(){
    $("#precoVendaNoMercado, #descPromo , #descFinan ").change (function(){
        if($("#precoVendaNoMercado").val() != "" && $("#descPromo").val() != "" && $("#descFinan").val() != "") {
            precoDeVendaRealizado = parseFloat($("#precoVendaNoMercado").val() * (1- (parseFloat($("#precoVendaNoMercado").val())) + parseFloat($("#precoVendaNoMercado").val())) / 100 );
            console.log('Calculo:'+precoDeVendaRealizado);
        }
    });
});

I did what you suggested @Diego Souza. And in the chrome console points to the line:

var desFinan = descFinan.replace('.', '').replace(',', '.'); precoDeVendaRealizado = precoVeNoMerc * (1 - (desPromo + desFinan) / 100 ); 
Saying "Uncaught TypeError: Can not read property 'replace' of undefined"

    
asked by anonymous 29.05.2015 / 16:07

3 answers

2

You should replace the "," with "." or you should convert the values to float:

var valorConvertido = parseFloat($("#precoVendaNoMercado").val());
    
29.05.2015 / 16:18
2

Remove all points and commas from the string by transforming it into an integer, then divide by a hundred, bringing the decimal places in numerical form.

jQuery(document).ready(function($){
    $("#go").click(function(event){
        event.preventDefault();
        if(
          $("#precoVendaNoMercado").val() != "" && 
          $("#descPromo").val() != "" && 
          $("#descFinan").val() != ""
        ) {
          // Trabalhe com uma variável, é sempre melhor
          var valor = $("#precoVendaNoMercado").val();
            
          if (valor.indexOf(",") == -1) { // verifica se tem casas decimais
             valor += ",00"; // Se não tiver coloca
             var dec = 2; // Duas casas decimais
             var ndec = 100; // 100 para duas casas decimais (10 para 1, 1000 para 3)
              
            } else {
                // Calcula para quanto tem que dividir para voltar as casas decimais
                var dec = valor.substring(valor.indexOf(",")+1).length; // Casas decimais
                var ndec =  Math.pow(10, dec); // Valor para calcular (10 elevado ao número de casas decimais)
            }

          // Remove pontos, vírgulas e espaços em branco
          valor = valor.replace(/\,\./, "").trim();
          
          // Transforma para numérico inteiro e depois divide para o quantidade de casas decimais
          valor = parseInt(valor) / ndec;
            
          // Calcula a formula
          var precoDeVendaRealizado = valor * (1 - valor + valor) / 100 ;
          // Transforma em string voltando o divisor decimal para vírgula
          var visivel = precoDeVendaRealizado.toString().replace('.',',');

$("#result").text('Calculo: '+visivel);
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputid="precoVendaNoMercado" value="54,21"> <button id="go">GO</button>
<p id="result"></p>

I did not quite understand the purpose of the formula, but the way it is could be summarized as valor / 100 .

    
29.05.2015 / 16:43
1

You should create a function or do a routine that takes the commas out. Comma is considered text (string) and does not make calculations with texts.

But the point is to replace the comma with a dot. And take the points out of the thousands.

replace can help you.

var valor = '190.000,00';
var valorFormatado = valor.replace('.', '').replace(',', '.');

Then you can do the calculation, since the point is not considered as text if it is not enclosed in quotation marks.

    
29.05.2015 / 16:12