0

I am using Globalizejs to format Currency based on Logged-In user details in my application.

I don't want currency Symbol to be displayed when formatting is done using below code snippet:

Globalize.locale( "en" );
currencyFormatter = Globalize.currencyFormatter( "USD", {
  maximumFractionDigits: 0,
});

currencyFormatter(parseInt(totalCost.amount));

which returns

$1,212,122,112 for amount: 1212122112

Is there any option similar to maximumFractionDigits to avoid the currency symbol ?

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Is `totalCost.amount` a variable from server or from user? Note that you should use parseNumber if it's from user (since it could be localized). – Rafael Xavier Apr 28 '17 at 12:26
  • Are you intentionally using `parseInt`, i.e., is amount an integer and you would be stripping off any potential fractions available there. PS: If it's the amount of cents I would expect decimal shift by dividing the amount by a factor of 10^x. – Rafael Xavier Apr 28 '17 at 12:28
  • @RafaelXavier Yes totalCost is an Object which I am getting from the Server. – Neeraj Jain May 02 '17 at 07:05
  • Why is it a string and not a number already? Is it a string representing a POSIX number or a localized number? – Rafael Xavier May 02 '17 at 12:25
  • I am getting this data from an API call to which I have no or less control, hence it's in the String format. – Neeraj Jain May 03 '17 at 03:48

1 Answers1

1

Short answer: Globalize.numberFormatter

Longer answer: Two benefits of using currency formatter is: (a) have the currency symbol properly formatted, and (b) have the appropriate number of fraction digits properly formatted; note that several currencies such as USD, EUR, have 2 fraction digits by default, but others like JPY have 0, there are different cases too.

The appropriate solution to customize the markup and style of a formatted output is to use parts Globalize.currencyToPartsFormatter: At the time we speak, this feature isn't implemented yet https://github.com/globalizejs/globalize/issues/679.

As a workaround, which should work fine for your specific use case (no currency symbol + integers only amount), using Globalize.numberFormatter should suffice.

Rafael Xavier
  • 2,840
  • 27
  • 34
  • When are you planning to implement mentioned feature `Currency format to parts #679` Are you saying there is no difference between **currencyFormatter** and **numberFormatter** apart from the *currency Symbol* ? – Neeraj Jain May 02 '17 at 07:08
  • No, as written above, currencyFormatter also figures out the default fraction digits of the requested currency. There is no roadmap for 679, but PR is welcome and should be accepted. – Rafael Xavier May 02 '17 at 12:23
  • okay, thanks. will try to create the PR....... one more question Is there a way to configure the currencyFormatter to show the currencySymbol as prefix, not as a suffix? – Neeraj Jain May 03 '17 at 04:21
  • No by design. Prefix/suffix depends on the locale – Rafael Xavier May 03 '17 at 12:55
  • @NeerajJain in general, does this answer your question? – Rafael Xavier May 04 '17 at 14:08
  • Yes, indeed i was able to complete my task with NumberFormatter – Neeraj Jain May 12 '17 at 07:03