Sunday, August 18, 2013

JavaScript - Regex to format currency

A simple way to format currency! Given a number, format it to a currency-esque format.

number.toFixed(decimalPlaces).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");

Sample:


Symbol (EG: R, $):
Number (EG: 1234567.50):
Decimal Places (EG: 1, 2, 3 - Probably 2):

1 comment :