Valor monetario em java script

1 resposta
rhapsody

ae to com um problema criei uma function em java script para tranformar numeros interos em formato de dinhero mas ela nao retorna os centavos. alguem pode me ajudar?

function calcularTroco(valorTotal){
        var valor = document.getElementById("tf_troco").value;
        var troco = valor - valorTotal;
        var cents;
        var trocod;
        var x;
        valor = valor.replace(",",".");
        x = 0;
    
       if(troco<0) {
          troco = Math.abs(num);
          x = 1;
       }
    
   		 if(isNaN(troco)) troco = "0";
        	  cents = Math.floor((troco*100+0.5)%100);
       		  troco = Math.floor((troco*100+0.5)/100).toString();
    
      	 if(cents < 10) cents = "0" + cents;
        	  for (var i = 0; i < Math.floor((troco.length-(1+i))/3); i++)
            	   troco = troco.substring(0,troco.length-(4*i+3))+'.'
                   +troco.substring(troco.length-(4*i+3));
    
        		   trocod = troco + ',' + cents;
    
        	if (x == 1) trocod = ' - ' + trocod;
        
        		var exibeTroco = "  <font color='red'>Troco:R$ " + trocod + "</font>";
        		document.getElementById("labelTroco").innerHTML = exibeTroco;
    }

1 Resposta

rhapsody

aee ja cpncertei o burrice esqueci de da replace no valor pra subistitui , por ponto :evil:

Criado 20 de maio de 2009
Ultima resposta 20 de mai. de 2009
Respostas 1
Participantes 1