3

I have an integer stored as US cents, ie 1700 cents. How can I convert that to a string that is $17.00 using javascript? I have tried toFixed and Intl.NumberFormat but they return $1700?

2
  • Have you tried dividing the value by 100 first...? Commented May 15, 2022 at 10:41
  • Can you show the way in which you used toFixed() and Intl.numberFormat? Commented May 15, 2022 at 10:41

3 Answers 3

5

You can use toLocaleString() function for this.

var cents = 1629;
var dollars = cents / 100;
dollars = dollars.toLocaleString("en-US", {style:"currency", currency:"USD"});
console.log(dollars);

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

Comments

0

Another solution could be this:

var cents = 1629
var currency = cents/100
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(currency))
console.log(new Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR' }).format(currency))

Comments

0

you can also try this simple javascript number format

const currencyConverter = (amount) => {
    let dollars = amount/ 100;
    const result = Number(dollars).toLocaleString("en-US");    
    return result;
}

now you have a reusable currency converter function which to use you just have to call and then pass the number into the function

currencyConverter(1700)

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.