Rizstien
Rizstien

Reputation: 802

How to format a number into standard currency format (comma seperated digits)

I want to know how can i format a number like 2000000 into standard currency format i.e 2,000,000 using Java code or jQuery ? Javacode is best but jQuery will also work Thanks

Upvotes: 1

Views: 5265

Answers (3)

davidcondrey
davidcondrey

Reputation: 35973

/**
 * Format currency to $0.00 format
 * @param  {[string]} price
 * @return {[string]} Formated Price
 */
util.formatCurrency = function(price) {
    return '$' + String(price).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};

Upvotes: 0

Sergio Machado
Sergio Machado

Reputation: 11

Use NumberFormat.getCurrencyInstance(). Consider this:

int n = 2000000;

NumberFormat usa = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat uk = NumberFormat.getCurrencyInstance(Locale.UK);
NumberFormat germany = NumberFormat.getCurrencyInstance(Locale.GERMANY);

System.out.println(usa.format(n));
System.out.println(uk.format(n));
System.out.println(germany.format(n));

Upvotes: 0

Konerak
Konerak

Reputation: 39763

Have a look at the jQuery number formatter. Most formats are already there, and you can define your own.

Upvotes: 1

Related Questions