Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Array Manipulation Transform Array Items with map()

Tim Strand
Tim Strand
22,458 Points

Why use toFixed instead of toLocaleString?

is there a good case to be made for using toFixed over toLocaleString for creating a currency string? it seems like using toLocaleString has more versatility for different denominations etc even if the logic is more burdensome initially.

const prices = [5, 4.23, 6.4, 8.09, 3.20];

// Result: [ '$5.00', '$4.23', '$6.40', '$8.09', '$3.20' ]

const displayPrices = prices.map(price => parseFloat(price).toLocaleString('en-US', {style: 'currency', currency: 'USD', currencyDisplay: 'symbol', minimumFractionDigits: 2, maximumFractionDigits: 2});

1 Answer

Steven Parker
Steven Parker
231,268 Points

If your application is intended for multiple currencies, then toLocaleString is clearly the better choice.