-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.5.1EnglishNum.rb
127 lines (99 loc) · 2.62 KB
/
8.5.1EnglishNum.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def englishNumber number
if number < 0 # No negative numbers
return 'Please enter a number zero or greater.'
end
if number == 0
return 'zero'
end
numString = '' # This is the returned String
onesPlace = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tensPlace = ['ten', 'twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
#left is how much of the nubmer we still have left to write out.
#"write" is the parte we are writing out right now.
#trillions
left = number
write = left / 1000000000000
left = left - write * 1000000000000
if write > 0
trillions = englishNumber write
numString = numString + trillions + ' trillion '
if left > 0
numString = numString + ' '
end
end
#billions
write = left / 1000000000
left = left - write * 1000000000
if write > 0
billions = englishNumber write
numString = numString + billions + ' billion '
if left > 0
numString = numString + ' '
end
end
#millions
write = left / 1000000
left = left - write * 1000000
if write > 0
millions = englishNumber write
numString = numString + millions + ' million '
if left > 0
numString = numString + ' '
end
end
#thousands
write = left / 1000
left = left - write * 1000
if write > 0
thousands = englishNumber write
numString = numString + thousands + ' thousand '
if left > 0
numString = numString + ' '
end
end
#hundreds
write = left / 100
left = left - write * 100
if write > 0
hundreds = englishNumber write
numString = numString + hundreds + ' hundred '
if left > 0
numString = numString + ' '
end
end
#tens
write = left / 10
left = left - write * 10
if write > 0
if ((write == 1) && (left > 0))
numString = numString + teenagers[left - 1]
left = 0
else
numString = numString + tensPlace[write - 1]
end
if left > 0
numString = numString + '-'
end
end
write = left
left = 0
if write > 0
numString = numString + onesPlace[write - 1]
end
numString
end
puts englishNumber( 0)
puts englishNumber( 9)
puts englishNumber( 10)
puts englishNumber( 11)
puts englishNumber( 17)
puts englishNumber( 32)
puts englishNumber( 88)
puts englishNumber( 99)
puts englishNumber(100)
puts englishNumber(101)
puts englishNumber(234)
puts englishNumber(3211)
puts englishNumber(99999)
puts englishNumber(100000000000)