-
Notifications
You must be signed in to change notification settings - Fork 0
/
commify.rb
75 lines (60 loc) · 2.12 KB
/
commify.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Precision
# what character should be displayed at each breakpoint?
COMMIFY_DELIMITER = ','
# What should the decimal point character be?
COMMIFY_DECIMAL = '.'
# What power of 10 defines each breakpoint?
COMMIFY_BREAKPOINT = 3
# Should an explicit '0' be shown in the 100ths place,
# such as for currency?
COMMIFY_PAD_100THS = true
=begin rdoc
This method returns a <b>String</b> representing the numeric value of
self, with delimiters at every digit breakpoint. 4 Optional arguments:
1. delimiter (<b>String</b>): defaults to a comma
2. breakpoint (<b>Integer</b>): defaults to 3, showing every multiple of 1000
3. decimal_pt (<b>String</b>): defaults to '.'
4. show_hundredths (<b>Boolean</b>): whether an explicit '0' should be shown
in the hundredths place, defaulting to <b>true</b>.
=end
def commify(args = {})
args[:delimiter] ||= COMMIFY_DELIMITER
args[:breakpoint] ||= COMMIFY_BREAKPOINT
args[:decimal_pt] ||= COMMIFY_DECIMAL
args[:show_hundredths] ||= COMMIFY_PAD_100THS
int_as_string, float_as_string = to_s.split('.')
int_out = format_int(
int_as_string,
args[:breakpoint],
args[:delimiter]
)
float_out = format_float(
float_as_string,
args[:decimal_pt],
args[:show_hundredths]
)
return int_out + float_out
end
private
=begin rdoc
Return a <b>String</b> representing the properly-formatted
<b>Integer</b> portion of self.
=end
def format_int(int_as_string, breakpoint, delimiter)
reversed_groups = int_as_string.reverse.split(/(\d{#{breakpoint}})/)
reversed_digits = reversed_groups.grep(/\d+/)
digit_groups = reversed_digits.reverse.map { |unit| unit.reverse }
return digit_groups.join(delimiter)
end
=begin rdoc
Return a <b>String</b> representing the properly-formatted
floating-point portion of self.
=end
def format_float(float_as_string, decimal_pt, show_hundredths)
return '' unless float_as_string
output = decimal_pt + float_as_string
return output unless show_hundredths
output += '0' if (float_as_string.size == 1)
return output
end
end