1

I am displaying currency values in javascript, I want to display $ with every value and I also want , (comma) for thousands but I don't want rounding of digits after decimal point and I also don't have a fixed limit of how many digits would be after decimal point.

It is for en-AU

for example

45000 -> $45,000

3.6987 -> $3.6987

3 -> $3

4.00 -> $4.00

Is there any built-in JavaScript method or library can help to achieve this?

Ali
  • 1,015
  • 14
  • 40
  • This is a question and answer site. What is the question? Where is your attempt to solve this yourself? – charlietfl Nov 09 '17 at 02:44
  • show us the code where do you want all those things? – Dhaval Jardosh Nov 09 '17 at 02:44
  • Possible duplicate of [How can I format numbers as dollars currency string in JavaScript?](https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-dollars-currency-string-in-javascript) – crimson589 Nov 09 '17 at 02:46
  • 1
    US icurrency doesn't have more than 2 decimal positions, so `$3.6987` is not a valid currency amount and shouldn't be displayed as one. The smallest US currency is 1 cent (1/100 of a dollar), which would be displayed as `$0.01`. More than two decimals is going to do nothing but confuse people, because you're pretending something is a currency amount that is not. – Ken White Nov 09 '17 at 02:59

3 Answers3

7

I Suggest to use Intl.NumberFormat

var formatter = new Intl.NumberFormat('en-US', {
   style: 'currency',
   currency: 'USD',
   minimumFractionDigits: 2,      
});

formatter.format(3242); /* $3,242.00 */

You can config your FractionDigits and even your currency sign :

var formatter = new Intl.NumberFormat('en-US', {
   style: 'currency',
   currency: 'GBP',
   minimumFractionDigits: 4,      
});

formatter.format(3242); /* £3,242.0000 */

UPDATE :

if you can't fixed your fraction digits you can use maximumFractionDigits and give it an amount of 20 and also give minimumFractionDigits value of 0 :

var formatter = new Intl.NumberFormat('en-US', {
   style: 'currency',
   currency: 'GBP',
   minimumFractionDigits: 0,
   maximumFractionDigits: 20,
});

formatter.format(3242.5454); /* £3,242.5454 */
Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44
  • @Ali I update the answer, use maximumFractionDigits and give it an amount of 20 and also give minimumFractionDigits value of 0 – Emad Dehnavi Nov 09 '17 at 03:07
4

You can use toLocaleString to add the commas to the number.

var number = 45000;
var formatted = '$' + number.toLocaleString(); // $45,000

number = 500999.12345;
formatted = '$' + number.toLocaleString(); // $500,999.12345

EDIT: To prevent rounding, use minimumFractionDigits option:

number.toLocaleString(undefined, { minimumFractionDigits: 20 });

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Related to this question.

justin
  • 439
  • 2
  • 10
1

Take a look at accounting.js, it has great features for formatting value in currency format.

Nitishkumar Singh
  • 1,781
  • 1
  • 15
  • 33