forked from codeunion/ruby-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommas.rb
More file actions
51 lines (40 loc) · 1.41 KB
/
commas.rb
File metadata and controls
51 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Method name: commas
# Inputs: A number, n
# Returns: A string representing the input, with commas inserted into the
# correct position.
# Prints: Nothing
# For example,
#
# commas(123) == "123"
# commas(1234) == "1,234"
# commas(12345) == "12,345"
# commas(1234567) == "1,234,567"
# Note #1
# If it's too much, don't worry about handling negative numbers at first.
# Note #2
# As always, focus first on how you would do this *as a human*. Imagine you
# has a piece of paper with a comma-less number on it. How would you decide to
# insert the commas? Which comma would you insert first?
def commas(num)
sign = num < 0 ? '-' : ''
num_array = num.abs.to_s.split('.')
integer_part = num_array[0].to_s
fractional_part = num_array[1].nil? ? '' : '.' + num_array[1].to_s
integer_part = integer_part.reverse.scan(/.{1,3}/)
integer_part_with_commas = integer_part.join(',').reverse
sign + integer_part_with_commas + fractional_part
end
if __FILE__ == $0
puts commas(123) == '123'
puts commas(1_234) == '1,234'
puts commas(12_345) == '12,345'
puts commas(1_234_567) == '1,234,567'
puts commas(-123) == '-123'
puts commas(-1_234) == '-1,234'
puts commas(-12_345) == '-12,345'
puts commas(-1_234_567) == '-1,234,567'
puts commas(-123.25) == '-123.25'
puts commas(-1_234.501) == '-1,234.501'
puts commas(-12_345.7_502) == '-12,345.7502'
puts commas(0) == '0'
end