How to transform a monetary value into Reals in a number 7743;ero float?

Asked

Viewed 172 times

-3

I need to turn a number that is in the format 20.000,00 (with points in each thousand) for a type number float, how do I do that?

 var value = $('#money').val()

 var currency = value.replace(",00", "")
 
 var money =   currency.replace(/[ ,.]/g, '');

  • 3

    Just one remark - Float for money, in virtually any situation, is terrible idea - Reading suggestion

  • How to represent a Javascript decimal then?

1 answer

3


Maybe this will help:

 var value = $('#money').val() // ex: 20.000,99
 var currency = value.replace(/\D/g, ''); // remove tudo que não é dígito, fica então 2000099
 var money =   parseFloat(currency)/100; // 20000.99

NOTE: This example works for values with two decimal places (the most common in Brazilian currency)

  • Thank you, thanks a lot

  • 1

    @Bacco you’re right. I’ll make it clear that the number must be formatted with two decimal places.

  • 1

    Better, I’ll take the previous comment not to generate confusion

Browser other questions tagged

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