ebick
ebick

Reputation: 13

How do I format a numeric element as currency in KnockoutJS or JavaScript

I have the following routine in a script tag.

        for (var i = 0, j = message.Transactions.length; i < j; i++) {
        var m = message.Transactions[i];

        //m.Amount Needs to be Currency
        IndexViewManager.displayList.push(m);
    }

There's an element in "m" that is numeric and I want it to be displayed as currency. KnockoutJS is being utilized to data-bind the elements if that helps.

Upvotes: 1

Views: 170

Answers (1)

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You can make use of the toLocaleString method and pass in the currency information as a object which is one of the input parameter to the method.

var number = 1234;

console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));

Upvotes: 1

Related Questions