1
Hello I’m not getting to make the Code accept cents follow the javascript code someone gives a light there
<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>
ascript
Have you tried instead of using
parseInt
useparseFloat
, and use the dot to separate the pennies instead of the comma?– JuniorNunes
So price = parseFloat (price.replace("," , ".") );
– Hemerson Prestes
Where does it not accept? You say it. By calculating the cents?
– Willian Coqueiro
that ! Once I add that to the parseFloat javascript (price.replace("," ".") ); but it doesn’t make the dynamic calculation of my table
– Hemerson Prestes
So it works price = parseint(price); however it does not visualize the pennies type what is 60,40 becomes only 60
– Hemerson Prestes
It won’t even calculate. Parseint is only for you to use integers.
– Willian Coqueiro
There at the beginning of the question script. Take out that parseint statement. You don’t have to keep formatting the value all the time. The value comes with a semicolon and how many houses it is, after calculating by the quantity it formats it with rounded preco_parseFloat = (price.toFixed(2));
– Willian Coqueiro
You can format it several times. But keep in mind that when multiplying or dividing by the amount it will generate you squares after the comma again. The important thing is to format after the calculation. And parseint is not for monetary calculations or calculations that use decimal places.
– Willian Coqueiro
value 60,90 with comma put so parseFloat (price.replace(',', '.') ) is a point in place of the comma I would like it to be comma
– Hemerson Prestes
Check out Hemerson: http://answall.com/questions/11018/como-representdinheiro-em-javascript/175800#175800
– Antonio Alexandre