The Regular Expression Behind Currency Formatting

I came across an interesting requirement today to format a floating number as currency. In the US and many other countries, currency greater than or equal to 1000 units ($1,000.00) is typically broken up into triads via a comma delimiter. My initial reaction was to use a recursive function, but I glanced at the Rails’ ActiveSupport gem to see how their number_with_delimiter method performed this same task. I was surprised to learn that this was handled via a clever regular expression.

View at https://coderwall.com/p/uccfpq

Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.