View monetary values simply

Asked

Viewed 56 times

0

good morning!

What would be the way plus simple to display monetary values with Javascript? I was able to limit the decimals using .toFixed(2). Only the whole part goes something like this 10510.20 I wish it were 10.520,20. Any suggestions?

  • 1

    Take a look at this function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

  • But this desired result would be the return of?

1 answer

1

Use this function always gave me a huge fix for this problem:

function format_num(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
  • It would not be x.toString(). replace(/ B(?= ( d{3})+(?! d)/g, "." ); Point instead of comma in g, ",") . If possible put a run in your answer to test

  • not because the author wanted in commas

  • No, he says "I wanted it to be 10,520.20" point mile separator and decimal comma

Browser other questions tagged

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