format currency in dynamic span

Asked

Viewed 62 times

0

I want to format a dynamic input type range result, which gives me a dynamic value in a span. I need to format this value for currency. Ex: 1000 to 1,000

html

<span id="exibePercent">1000</span>

<input style="width:200px;" type="range" class="custom-range" min="1000" max="25000" id="price" value="1000" oninput="getElementById('exibePercent').innerHTML = this.value">

1 answer

1


You can format the value for currency with the function toLocaleString() that is present in the javascript class or int alias.

var valor = 55;
valor = valor.toLocaleString('pt-BR', {style :"currency", currency : 'BRL'});
//output: "R$ 55,00"

In case you need to format only those for formatted number without R$, Just remove the function parameters

var valor = 55555;
valor = valor.toLocaleString('pt-br', {minimumFractionDigits: 2}));
//output: "55.555,00"

In your case above would be

<input style="width:200px;" type="range" class="custom-range" min="1000" max="25000" id="price" value="1000" oninput="getElementById('exibePercent').innerHTML = parseInt(this.value).toLocaleString('pt-BR', {style :'currency', currency : 'BRL'})"

Browser other questions tagged

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