-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016-dollar-format.rb
62 lines (51 loc) · 1.42 KB
/
2016-dollar-format.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
# https://programmingpraxis.com/2016/09/30/dollar-format/
#
# Dollar Format
#
# We have a simple task today, a function that formats a number in dollar format.
# A number like 1234567.8912 should be rounded to two positions after the decimal
# point, have commas inserted every three positions before the decimal point, and
# have a dollar sign prepended; thus, the function should format 1234567.8912 as
# $1,234,567.89.
def format(amount)
decimal, fraction = amount.round(2).to_s.split('.')
decimal_part = decimal.
chars.
reverse.
each_slice(3).
map { |xs| xs.reverse.join }.
reverse.
join(",")
"$" + decimal_part + "." + fraction.ljust(2, "0")
end
def test(method, input, target)
output = self.send(method, *input)
puts method.to_s
puts "input: #{input}"
puts "target: #{target}"
puts "output: #{output}"
puts "pass: #{output == target}"
puts
end
test :format, 1234567.8912, "$1,234,567.89"
test :format, 234567.8912, "$234,567.89"
test :format, 1, "$1.00"
test :format, 23.45 , "$23.45"
test :format, 0.50 , "$0.50"
test :format, 99.01 , "$99.01"
__END__
Submission
In Ruby
[sourcecode lang="ruby"]
def format(amount)
decimal, fraction = amount.round(2).to_s.split('.')
decimal_part = decimal.
chars.
reverse.
each_slice(3).
map { |xs| xs.reverse.join }.
reverse.
join(",")
"$" + decimal_part + "." + fraction.ljust(2, "0")
end
[/sourcecode]