gFontaniva
gFontaniva

Reputation: 903

Fomating decimal values JavaScript

I need format a decimal number on JavaScript, my number are returning 100.000 but I want 100,00.

var val = 100.0000;
console.log(val);
// do something
console.log(val);

what I get: 100.0000
what I expected: 100,00

what I need to do for made this?

Upvotes: 0

Views: 65

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386846

It depends on the need. I suggest to have a look to the formatting methods of Number.prototype.toLocaleString.

var val = 100.0000;
document.write(val.toLocaleString('de-DE', { maximumFractionDigits: 2, minimumFractionDigits: 2}));

Upvotes: 0

Kristian Vitozev
Kristian Vitozev

Reputation: 5971

One possible solution:

parseFloat(val).toFixed(2).replace('.', ',');

See in action: JSFiddle

Upvotes: 2

Related Questions