-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfmtnum
More file actions
executable file
·75 lines (67 loc) · 1.47 KB
/
fmtnum
File metadata and controls
executable file
·75 lines (67 loc) · 1.47 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# tlehman
#
# Usage:
# $ fmtnum "$(echo 7^7^2 | bc)"
# 256,923,577,521,058,878,088,611,477,224,235,621,321,607
#
# $ echo 7^7^2 | bc | fmtnum
# 256,923,577,521,058,878,088,611,477,224,235,621,321,607
DIGITS = %w{one two three four five six seven eight nine ten}
BIG_NUMBERS = {
2 => 'hundred',
3 => 'thousand',
6 => 'million',
9 => 'billion',
12 => 'trillion',
15 => 'quadrillion',
18 => 'quintillion',
21 => 'sextillion',
24 => 'septillion',
27 => 'octillion',
30 => 'nonillion',
33 => 'decillion',
36 => 'undecillion',
39 => 'duodecillion',
42 => 'tredecillion',
45 => 'quattuordecillion',
48 => 'quindecillion',
51 => 'sexdecillion',
54 => 'septendecillion',
57 => 'octodecillion',
60 => 'novemdecillion',
63 => 'vigintillion',
66 => 'unvigintillion',
69 => 'duovigintillion',
72 => 'trevigintillion',
75 => 'quattuorvigintillion',
78 => 'quinvigintillion',
81 => 'sexvigintillion',
84 => 'septenvigintillion',
87 => 'octovigintillion',
90 => 'novemvigintillion',
93 => 'trigintillion',
96 => 'untrigintillion',
99 => 'duotrigintillion',
100 => 'googol'
}
def usage
<<-EOF
Usage:
$ fmtnum 20100102030304
20,100,102,030,304
EOF
end
def number?(input)
!input.nil? && (input =~ /^(\d+)(\.\d+)?$/)
end
def format(n)
n.gsub(/(?<=\d)(?=(\d\d\d)+(\b|\.))/, ',')
end
input = ARGV.first
input = STDIN.read if input.nil?
if number?(input)
puts format(input)
else
puts usage
end