Forum How do I...?

toLocaleString() returns Object instead of String?

quiredan
I'm generating a PDF using code that includes some Javascript for calculating and displaying costs, but when I use Number.toLocaleString(), it returns "[object Number]" instead of the number as a string.

The code looks something like this:

var element = document.querySelector('.selector');
var cost = 5000.00;

element.textContent = cost.toLocaleString('en-US', { style: 'currency', currency: 'USD' });

// expected result: "$5,000.00"
// actual result: "[object Number]"


Any idea why this wouldn't work as expected? I see toLocaleString() in the Javascript Support documentation, but it seems to be exhibiting the behavior of Object.toLocaleString() when it is NOT overridden for the Number object?
mikeday
Yes, currently toLocaleString is only supported for Object, not Number or other classes, sorry. I can add this to the roadmap for future attention.
quiredan
Thank you!