Remove the R$ from the tolocalestring

Asked

Viewed 495 times

0

Good afternoon, everyone,

I use the following command to transform a float number for money:

parseFloat(valor).toLocaleString('pt-br',{style: 'currency', currency: 'BRL'})

This works, and this value I use for the display in a report. However, in this report he puts the R $ before the value, which the report is good, but not to save in the bank.

I needed to know if there is a way to take a formatted value in this way and remove this R$, keeping only the number.

I thank anyone who can help.

2 answers

5


Just do not format as "currency", and set the amount of decimals to 2:

let valor = '123456789.947';
console.log(parseFloat(valor).toLocaleString('pt-br',
            { minimumFractionDigits: 2, maximumFractionDigits: 2 }));

// apenas para comparar com currency
console.log(parseFloat(valor).toLocaleString('pt-br',
            { style: 'currency', currency: 'BRL' }));

The locale "pt-BR" keeps the settings of the thousands separator and decimal places (respectively, the semicolon), simply matching the number of decimal places.

toLocaleString uses the same options of Intl.NumberFormat, see the documentation to know all the options.

Remember that if you have more than 2 decimal places, the value will be rounded, as you can see in the example above.


But if you want to save in the database, do not save formatted, prefer to save only the same number (the numeric value). The formatted value serves only for presentation purposes (for example, in the database you save the numeric value 1000 and only to present on the screen you format as 1.000,00).

  • Thank you very much!

5

Using Intl.NumberFormat also solves, an advantage over the use of this API is like the @fernandosavio commented, you can create a whole format and save in a variable, so you can reuse in several places without having to set the formatter, as it is in the toLocaleString:

var number = 1000.01;
var formatter = new Intl.NumberFormat('pt-BR');

console.log(formatter.format(number));
console.log(formatter.format(100000));
console.log(formatter.format(99999.99));

Another advantage of Intl.NumberFormat is the performance, in accordance with MDN:

When formatting large Numbers of Numbers, it is Better to create a Numberformat Object and use the Function provided by its Numberformat.format Property.

Take an online performance test: https://jsbench.me/igkfvly9nf/1, toLocaleString was 98% slower than Intl.NumberFormat, outworking:

Intl.NumberFormat foi 98% mais rápido

  • 1

    The advantage of this approach is that you can save the "Formatter" object in a variable and use it elsewhere in the code.. + 1

  • @fernandosavio this is great even, a huge hand on the wheel and as the MDN is also more performatico, probably for this reason you mentioned!

  • I commented before your issue, but I think the reason is this.. not to mention that can better organize the code

  • @fernandosavio blz! I added this more clearly in the reply, thank you!

  • I was going to comment on that in my reply but you went faster :-)

Browser other questions tagged

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