Customize . tofixed to use comma as decimal separator

Asked

Viewed 809 times

1

I need to get the .tofixed(2) work with commas and not with stitch as decimal separator.

In this case, when I calculate the final result is "501.60" and the right one would be "501.60".

document.getElementById('resultado'+tipo+'_'+index).value = (Valor1 * Valor2).toFixed(2);
  • Use replace after the tofixed to swap the point for the comma ex: tofixed(2). replace(".",",");

1 answer

3


If you are working with coins, the recommendation is to use toLocaleString.

var numero = 250.8 * 2;

console.log(parseFloat(numero.toFixed(2)).toLocaleString('pt-BR', {
  currency: 'BRL',
  minimumFractionDigits: 2
}));

// Output: 501,60

Or else...

var numero = 250.8 * 2;

console.log(parseFloat(numero.toFixed(2)).toLocaleString('pt-BR', {
  currency: 'BRL',
  style: 'currency',
  minimumFractionDigits: 2
}));

// Output: R$501,60

Otherwise, you can implement replace (as quoted in the question’s comment)

  • I’ve already solved the problem, thanks for your help ^^

Browser other questions tagged

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