Customize .tofixed to use comma as decimal separator

1

I need to make .tofixed(2) work with commas and not with dot as a decimal separator.

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

document.getElementById('resultado'+tipo+'_'+index).value = (Valor1 * Valor2).toFixed(2);
    
asked by anonymous 21.12.2017 / 10:44

1 answer

4

If you're working with currencies, 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 even ...

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 comment)

    
21.12.2017 / 11:07