javascript moeda reais 1

javascript moeda reais

var atual = 600000.00;

//com R$
var f = atual.toLocaleString('pt-br',{style: 'currency', currency: 'BRL'}).replace(".", ",");

//sem R$
var f2 = atual.toLocaleString('pt-br', {minimumFractionDigits: 2}).replace(".", ",");

Here is what the above code is Doing:
1. We’re creating a variable called atual and assigning it the value 600000.00.
2. We’re creating a variable called f and assigning it the value of atual.toLocaleString(‘pt-br’,{style: ‘currency’, currency: ‘BRL’}).replace(“.”, “,”).
3. We’re creating a variable called f2 and assigning it the value of atual.toLocaleString(‘pt-br’, {minimumFractionDigits: 2}).replace(“.”, “,”).

The first line of code is pretty straightforward. We’re creating a variable called atual and assigning it the value 600000.00.

The second line of code is a bit more complicated. We’re creating a variable called f and assigning it the value of atual.toLocaleString(‘pt-br’,{style: ‘currency’, currency: ‘BRL’}).replace(“.”, “,”).

The toLocaleString() method is a built-in JavaScript method that converts a number into a string, representing the number in the specified locale.

The first argument of the toLocaleString() method is the locale. In our case, we’re using the locale pt-br, which stands for Portuguese (Brazil).

The second argument of the toLocaleString() method is an object that specifies the options for the number. In our case, we’re using the style option to specify that we want the number to be formatted as currency. We’re also using the currency option to specify that we want the currency to be BRL, which stands for Brazilian Real.

The replace() method is a built-in JavaScript method that replaces a part of a string with another string. In our case, we’re replacing the period with a comma.

The third line of code is similar to the second line of code. We’re creating a variable called f2 and assigning it the value of atual.toLocaleString(‘pt-br’, {minimumFractionDigits: 2}).replace(“.”, “,”).

The only difference is that we’re using the minimumFractionDigits option to specify that we want the number to have at least two decimal places.

Similar Posts