0

I'm working with Stripe Checkout, and feed the amount as a number like so 11999. How can I show this number as a string like $119.99?

I tried new Intl.NumberFormat('en-IN', { currency: 'USD' }).format(11999)

But it's show as $ 11,999

I tried http://numeraljs.com as well, but same issue. Any one have an idea?

7
  • What's the problem with the current format? Commented May 29, 2019 at 7:43
  • but how can it add decimal to your number? Commented May 29, 2019 at 7:47
  • 5
    .format(11999 / 100) by converting cents to dollars first? Commented May 29, 2019 at 7:47
  • the input is problematic. 11999 is 11 thousand 999 hundred Commented May 29, 2019 at 7:47
  • Thanks @adiga. I feel stupid for not thinking of that. Commented May 29, 2019 at 7:49

2 Answers 2

3

Firstly, change the cents to dollars by dividing by 100 (the number of cents in a dollar). Then prepend the $.

const num = 11999;
const res = "$" + (num / 100);
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it in couple of ways.

First way is to parse it as a number and than divide it by 100 but that has a downside if your number has more than 2 decimal numbers.

let format = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
});

format.format(parseFloat(11999)/100);

If your number has more decimal points you can do something like:

let numberOfDigits = 2;
format.format(parseFloat(11999)/numberOfDigits*10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.