how to convert a number to real (money)?

Asked

Viewed 44 times

-1

Follows the code

var n1 = 155551.15
document.write(`${n1.toFixed(2).replace('.' ,  ',')}`)
n1.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })

But at the time of seeing the number does not change to real ... Some help ?

  • You want to display in HTML the value of n1 in real R format$?

  • exact, but on the page the number becomes normal and does not change the symbol of real

1 answer

1


You actually wrote down the value of n1 in HTML with the method toFixed, while should have done it with the method toLocaleString.

The use of the method toLocaleString is correct, but you used it and did nothing with the return.

let n1 = 155551.15;

document.write(`${n1.toLocaleString('pt-br', { style: 'currency', currency: 'BRL' })}`);

The excerpt of the code with the toFixed is not necessary.

  • Thank you, then the use of . tofixed and . replace is already done in the correct toLocaleString method ?

  • It already does the complete formatting: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

  • Thank you very much !!!

Browser other questions tagged

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