Accept cents in javascript

Asked

Viewed 592 times

0

I would like this code to take the cents of the value coming from the javascript variable

preco = parseInt(preco);

I’ve put nothing like that

preco = parseFloat(preco);

Complete code below

total = 0;

function adiciona(id) {
    calcula(id, "adicao");
}

function remove(id) {
    calcula(id, "subtracao");
}

function calcula(id, operacao) {
    nomeid = "nome" + id;
    precoid = "preco" + id;
    qtdid = "qtd" + id;
    nome = document.getElementById(nomeid).innerHTML;

    preco = document.getElementById(precoid).innerHTML;
    preco = parseInt(preco);

    qtd = document.getElementById(qtdid).innerHTML;
    qtd = parseInt(qtd);

    //Debug
    //alert("Produto: " + nome + "\n Preço: " + preco);    

    if (operacao == "adicao") {
        total = total + preco;
        qtd = qtd + 1;
    } else {
        total = total - preco;
        qtd = qtd - 1;
    }

    document.getElementById(qtdid).innerHTML = qtd;
    document.getElementById("total").innerHTML = total;
}
  • What gives alert(document.getElementById(precoid).innerHTML);? and you just want to get the decimal part, that’s it?

  • for example the value this 60,80 only appears 60 understood

2 answers

1


Fala Hemerson,

Try the following:

// com preco ainda como string converte vírgula para ponto
preco.replace (", ", ". "); 

// depois converte para float
preco = parseFloat(preco); 

And in php you’ll need to use number_format to format when you want to display comma float as decimal home. http://php.net/manual/en/function.number-format.php

  • that and in php or javascript ?

  • The code I wrote above is javascript. I can not see here your code of yesterday to guide you better taking it into account because I am in the house of my bride writing in the cell... :-]

  • Let’s go to chat ?

  • I’ll put the full code below

0

 <script>
    
	    total = 0;
        
        function adiciona(id)
        {
            calcula(id,"adicao");
        }
        
        function remove(id)
        {
            calcula(id,"subtracao");
        }    
            
        function calcula(id,operacao)
        {
                nomeid  = "nome"+id;
                precoid = "preco"+id;
                qtdid   = "qtd"+id;
                
                nome  = document.getElementById(nomeid).innerHTML;
                
                preco = document.getElementById(precoid).innerHTML;    
                preco = parseInt(preco); 
                
                qtd   = document.getElementById(qtdid).innerHTML;
                qtd   = parseInt(qtd);

                //Debug
                //alert("Produto: " + nome + "\n Preço: " + preco);    
                
                if(operacao=="adicao")
                {
                    total = total + preco;
                    qtd = qtd + 1;
                }
                else
                {
                    total = total - preco;
                    qtd = qtd - 1;
                }
                
                document.getElementById(qtdid).innerHTML = qtd;
                
                document.getElementById("total").innerHTML = total;
        }    
            
        
    </script>
<script>
			function verifica_e_envia()
			{
				array_dados = new Array();
			
				colecao = document.getElementsByTagName("tr");
				
				qtd_blocos = colecao.length - 1; // O último tr da tabela é onde fica o total e está sendo descontado
				// É necessário saber a quantidade de blocos para poder usar em um loop catando os valores
				
				// Percorre os blocos catando nomes, quantidades e valores dos produtos com quantidade maior que zero
				for(i=1; i<=qtd_blocos ;i++)
				{
					qtdid = "qtd"+i;
					qtd   = document.getElementById(qtdid).innerHTML;
					qtd   = parseInt(qtd);
					
					if(qtd>0)
					{
						obj_tmp = {};
						
						nomeid = "nome"+i;
						nome   = document.getElementById(nomeid).innerHTML;
						
						precoid = "preco"+i;
						preco   = document.getElementById(precoid).innerHTML;
						preco   = parseFloat(preco);

						obj_tmp.nome  = nome;
						obj_tmp.preco = preco;
						obj_tmp.qtd   = qtd;
						obj_tmp.subtotal = qtd*preco;
						
						// adiciona elemento no array de dados que será enviado
						array_dados.push(obj_tmp);
					}
				}
				
				// põe o array_dados no input hidden json_dados
				document.getElementById("json_dados").value = JSON.stringify(array_dados);
				
				// envia o formulário form_pedido_produtos
				document.getElementById("form_pedido_produtos").submit();

			}
		</script>		

  • So, you need to convert string to float to do calculation and then float to string again to change point to comma.

  • As I said before hj I’m at my fiancée’s house and my help by cell is very limited, but there are a lot of good people on this site to help too.

  • Someone’s out there? I haven’t solved the question yet

  • Say, Hemerson, did you take a look at that answer of mine? http://answall.com/questions/11018/como-representdinheiro-em-javascript/175800#175800 Doesn’t suit you? If you want you can comment on how it could be improved.

  • Bro can’t you remember that agent was chatting on skype you reminded me to put the code to accept the pennies since la mano could not yet in that code you did have as an agent go to chat

  • I’m going to Skype

  • Turn on your Skype.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.