Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Paulo Roberto Start Corp

[Resolvido] somar valores em reais

Recommended Posts

Boa tarde

estou fazendo soma de campos R$ com JS

gostaria de saber como posso fazer para que quando a variavel estiver vasia receba o valor = 0

 

 
                   	if(valorA  == "") valorA  = 0;
					if(valorB  == "") valorB  = 0;
					if(valorC  == "") valorC  = 0;
					if(valorD  == "") valorD  = 0;
					if(valorE  == "") valorE  = 0;

 

 

 

<script type="text/javascript">
function id( el ){
   	return document.getElementById( el );
}
function getMoney( el ){
   	var money = id( el ).value.replace( ',', '.' );
   	return parseFloat( money )*100;
}
function soma()
{
   	var total = getMoney('valorA')-getMoney('valorB')+getMoney('valorC')+getMoney('valorD');
   	id('ValorTotal').value = ''+total/100;
}
</script>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Resolvido

 

 

 

    	function soma () {

                    	valorA         	= document.getElementById('valorA').value;
					 valorB         	= document.getElementById('valorB').value;
					 valorC         	= document.getElementById('valorC').value;
					 valorD         	= document.getElementById('valorD').value;


					 valorA   			=  valorA.toString  ().replace( ".", "" );
                    	valorA  			=  valorA.toString  ().replace( ",", "." );
					 valorB   			=  valorB.toString  ().replace( ".", "" );
                    	valorB   			=  valorB.toString  ().replace( ",", "." );
					 valorC   			=  valorC.toString  ().replace( ".", "" );
                    	valorC   			=  valorC.toString  ().replace( ",", "." );
					 valorD   			=  valorD.toString  ().replace( ".", "" );
                    	valorD   			=  valorD.toString  ().replace( ",", "." );


                   	if(valorA  == "") valorA  = 0;
					if(valorB  == "") valorB  = 0;
					if(valorC  == "") valorC  = 0;
					if(valorD  == "") valorD  = 0;

                   	ValorTotal   = parseFloat (valorA) - parseFloat (valorB) +parseFloat (valorC) + parseFloat (valorD) ;
					ValorTotal = ValorTotal.toFixed(2);
					ValorTotal = ValorTotal.toString().replace( ".", "," );

          	document.getElementById('ValorTotal').value = ValorTotal;

   	};

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.