Addie
Addie

Reputation: 1691

Parse a number using regex

I would like to take a number and format it as price (as a string). For example I want to take 250000 and display $250,000. How can I accomplish this using regex?

Upvotes: 1

Views: 216

Answers (4)

MZaragoza
MZaragoza

Reputation: 10111

you should be using the number_to_currency helper

number_to_currency(1234567890.50)                    # => $1,234,567,890.50
number_to_currency(1234567890.506)                   # => $1,234,567,890.51
number_to_currency(1234567890.506, precision: 3)     # => $1,234,567,890.506
number_to_currency(1234567890.506, locale: :fr)      # => 1 234 567 890,51 €
number_to_currency("123a456")                        # => $123a456

number_to_currency("123a456", raise: true)           # => InvalidNumberError

number_to_currency(-1234567890.50, negative_format: "(%u%n)")
# => ($1,234,567,890.50)
number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "")
# => £1234567890,50
number_to_currency(1234567890.50, unit: "£", separator: ",", delimiter: "", format: "%n %u")
# => 1234567890,50 £

for more info look at http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html

Upvotes: 0

ichigolas
ichigolas

Reputation: 7725

Matching with regex it's a pain in the #@! in this case since regex engines begin matching from the beginning of the string, not the end (finding 3 digit patterns beginning at the end of the string and going backwards). I'd suggest either doing something like this:

format_int = ->(s) do
  str = s.reverse.scan(/\d{1,3}/).join(',').reverse
  "$#{str}"
end

format_int['2500600'] => "$2,500,600"

... using Kernel#sprintf (this can be a little tricky) or, as you wanted: I was wrong, it can be achieved with regex as shown here and here.

Upvotes: 2

rvalvik
rvalvik

Reputation: 1559

For adding the commas you could give something like this a try:

/(\d)(?=(?:\d{3})+$)/

Then replace every match with \1,.

As such:

"12345512312".gsub(/(\d)(?=(?:\d{3})+$)/,'\1,')  => "12,345,512,312"

This would match any digit followed by an arbitrary number of 3 digit groups.

E.g. the first 2 in the above example is followed by 3 groups: 345, 512 and 312. The first 5 is followed by 2 groups: 512 and 312, etc.

Not sure if you'll be able to add the $ in the same regex though.

Upvotes: 5

AJcodez
AJcodez

Reputation: 34166

Try this one out (disclaimer- not a regex):

def prettify(n)
  int, dec = n.to_s.split('.')
  int = int.reverse.scan(/.{1,3}/).join(',').reverse
  [int, dec].reject(&:empty?).join('.')
end

Probably a gem out there for this kind of thing though

Upvotes: 2

Related Questions