Calculations with numbers after the comma and toFixed

Asked

Viewed 72 times

-1

I’m using toFixed in a little program I did to calculate a discount here in my work, but I can’t calculate if I use decimal numbers! If I put 150.50 for example, the program interprets and returns me the result of 150 integer, always...

var valorDoAbastecimento = parseInt(prompt("Digite o valor do abastecimento")) ;

var totalDeLitrosAbastecido = valorDoAbastecimento / 3.09

var descontoPorLitro = 0.10 ;

var totalDesconto = totalDeLitrosAbastecido * descontoPorLitro ;

var dinheiroPago = parseInt(prompt("Digite o valor pago em dinheiro."));

var valorTotalComDesconto = valorDoAbastecimento - totalDesconto

var troco = dinheiroPago - valorTotalComDesconto;



mostra ("<b>O total de litros abastecido é de: </b><mark><ins>" + totalDeLitrosAbastecido.toFixed(2) + " Litros") ;
mostra ("</mark><b></ins>O valor do desconto nesta compra é de </b><mark><ins>" + totalDesconto.toFixed(2) + " R$");
mostra ("<b></mark></ins>O Valor total da compra com o desconto fica:</b> " + "<mark><ins>" + valorTotalComDesconto.toFixed(2) + " R$")
mostra ("</mark></ins><b>O troco do cliente é de </b><mark><ins>" + troco.toFixed(2) + " R$");

2 answers

0

Your problem is what you’re doing parseInt(), but I would like to warn you that there is a method called toLocaleString().

About the Number​.prototype​.toLocale​String()

The method toLocaleString() returns a string with a language-sensitive representation of this number.

The new arguments locales and options allow applications to specify the language whose formatting conventions will be used and customize the behavior of the function. In previous implementations, which ignored the arguments locales and options arguments, the location used and the way to return the string were totally dependent on the implementation.

Basic example

var numero = 123456.789;

// informando um formato de moeda
console.log(numero.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// → 123.456,79 €

// o yen japonês não tem uma unidade menor
console.log(numero.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
// → ¥123,457

// limitando a três dígitos significativos
console.log(numero.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
// → 1,23,000

Read more about this subject at this link.

0

Are you wearing a parseInt(), it will convert the value to Integer. To convert the value to a Float use parseFloat(). But remember that the number should be typed with . (dot) as penny separator.

Browser other questions tagged

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