Jquery HTML Currency Mask (no input)

Asked

Viewed 375 times

0

Hello. I’m behind a coin mask made in jquery, but that applies directly to a span html tag, with a specific class. What else I have found in my searches are input masks for forms, which is not the case. I ended up getting a little confused, because my knowledge of JS is not much.

Then exemplifying a HTML

    <li>
    <span class="mylabel">Valor do Aluguel:</span>
    <span class="myvalue">25000000</span>
    </li>

In the example the script would transform this type of result 25000000 into -> 25.0000.00 That is, force the standard currency format from here in Brazil in a chosen field.

Logically following the same pattern for other types of values:
60050 -> 600,50
110000 -> 1.100,00
250035085 -> 2.500.350,85
etc..

Possible? How would?

Thank you in advance.

1 answer

1

You can do this through toLocaleString

$(".myvalue").text(parseInt($(".myvalue").text()).toLocaleString('pt-br', {
  style: 'currency',
  currency: 'BRL',
 
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li>
  <span class="mylabel">Valor do Aluguel:</span>
  <span class="myvalue">25000000</span>
</li>

  • 1

    Hello, thank you so much for the contribution. I think almost there, I noticed the peculiarity that in this case the final numbers are always zeroed. For example: 250035085 -> 2,500,350.00.

  • @Select to remove decimals use minimumFractionDigits: 0 as an option. If you have further questions check the documentation in Syntax https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Browser other questions tagged

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