/
string_formatting.koto
47 lines (41 loc) · 1.29 KB
/
string_formatting.koto
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
digits =
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
tens = '', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'
number_to_english = |n|
n = n.floor()
switch
n < 0 then 'minus ${number_to_english n.abs()}'
n < 20 then digits[n]
n < 100 then
x = (n / 10).floor()
y = n % 10
if y == 0
tens[x]
else
'${tens[x]}-${digits[y]}'
n < 1000 then
x = (n / 100).floor()
x_string = number_to_english x
y = n % 100
if y == 0
'$x_string hundred'
else
'$x_string hundred and ${number_to_english y}'
else '???'
@main = ||
n = match koto.args.get 0
null then 50
arg then arg.to_number()
result = -n..=n
.each |x| number_to_english x
.to_tuple()
if not (koto.args.get 1) == 'quiet'
print result
@tests =
@test number_to_english: ||
assert_eq (number_to_english 0), 'zero'
assert_eq (number_to_english -42), 'minus forty-two'
assert_eq (number_to_english 217), 'two hundred and seventeen'
assert_eq (number_to_english 999), 'nine hundred and ninety-nine'
assert_eq (number_to_english 123456), '???'