Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a number to split at every third digit

Tags:

ruby

I want to put in a space at every third character when formatting a number. According to this spec:

  it "should format an amount" do
    spaces_on( 1202003 ).should == "1 202 003"
  end

and I came up with this piece of code that does the job

  def spaces_on amount
    thousands = amount / 1000
    remainder = amount % 1000
    if thousands == 0
      "#{remainder}"
    else
      zero_padded_remainder = '%03.f' % remainder
      "#{spaces_on thousands} #{zero_padded_remainder}"
    end
  end

So my question is if this was the best way to do it. I suspect that there may be a regex way around it but I am not sure I will like the readability of that. (On the other hand - the %03.f magic is not very readable either....)

like image 348
froderik Avatar asked Feb 06 '12 20:02

froderik


4 Answers

>> def spaces_on number
>>   number.to_s.gsub(/\D/, '').reverse.gsub(/.{3}/, '\0 ').reverse
>> end
=> nil
>> spaces_on 12345678
=> "12 345 678"

Maybe there's an argument that regular expressions aren't amazingly readable, but personally I think they're simpler to understand than having to think in recursion.

like image 81
Gareth Avatar answered Nov 03 '22 22:11

Gareth


def spaces_on(number,sep=" ")
  number.to_s.tap do |s|
    :go while s.gsub!(/^([^.]*)(\d)(?=(\d{3})+)/, "\\1\\2#{sep}")
  end
end

NUMBERS = [ 1, 12, 123, 1234, 12345, 123456, 1234567,
            1.0, 1.2, 1.23, 1.234, 1.2345, 1.23456, 1.234567,
            12.3, 12.34, 12.345, 12.3456,
            123.4, 123.45, 123.456, 123.4567,
            1234.5, 1234.5, 1234.56, 1234.567,  1234.5678 ]

NUMBERS.each do |n|
  puts "%10s: %s" % [  n, spaces_on(n).inspect ]
  puts "%10s: %s" % [ -n, spaces_on(-n).inspect ]
end

Produces:

         1: "1"
        -1: "-1"
        12: "12"
       -12: "-12"
       123: "123"
      -123: "-123"
      1234: "1 234"
     -1234: "-1 234"
     12345: "12 345"
    -12345: "-12 345"
    123456: "123 456"
   -123456: "-123 456"
   1234567: "1 234 567"
  -1234567: "-1 234 567"
       1.0: "1.0"
      -1.0: "-1.0"
       1.2: "1.2"
      -1.2: "-1.2"
      1.23: "1.23"
     -1.23: "-1.23"
     1.234: "1.234"
    -1.234: "-1.234"
    1.2345: "1.2345"
   -1.2345: "-1.2345"
   1.23456: "1.23456"
  -1.23456: "-1.23456"
  1.234567: "1.234567"
 -1.234567: "-1.234567"
      12.3: "12.3"
     -12.3: "-12.3"
     12.34: "12.34"
    -12.34: "-12.34"
    12.345: "12.345"
   -12.345: "-12.345"
   12.3456: "12.3456"
  -12.3456: "-12.3456"
     123.4: "123.4"
    -123.4: "-123.4"
    123.45: "123.45"
   -123.45: "-123.45"
   123.456: "123.456"
  -123.456: "-123.456"
  123.4567: "123.4567"
 -123.4567: "-123.4567"
    1234.5: "1 234.5"
   -1234.5: "-1 234.5"
    1234.5: "1 234.5"
   -1234.5: "-1 234.5"
   1234.56: "1 234.56"
  -1234.56: "-1 234.56"
  1234.567: "1 234.567"
 -1234.567: "-1 234.567"
 1234.5678: "1 234.5678"
-1234.5678: "-1 234.5678"
like image 31
Phrogz Avatar answered Nov 04 '22 00:11

Phrogz


A small modification of the Gareth's solution (a more complex regexp, but no string reversing):

def spaces_on number
  number.to_s.gsub(/\d(?=(...)+$)/, '\0 ')
end

It works for negative numbers too (but doesn't work for floats).

like image 5
Alexis Avatar answered Nov 04 '22 00:11

Alexis


Is this Rails?

number_with_delimiter(@number, :delimiter => ' ')

This comes from ActionView::Helpers::NumberHelper.

If you want a standalone version that works with negative numbers and decimal point, use this:

def spaces_on number
  parts = number.to_s.split('.')
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ")
  parts.join(".")
end

The logic in the regex is this: Capture each digit that is followed only by groups of 3 digits (with no remaining digits), and output it with the space.

like image 3
Mark Thomas Avatar answered Nov 03 '22 23:11

Mark Thomas