Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active June 26, 2022 09:49
Show Gist options
  • Save toomasv/c3815c123fead2b6697d89be52aa111b to your computer and use it in GitHub Desktop.
Save toomasv/c3815c123fead2b6697d89be52aa111b to your computer and use it in GitHub Desktop.
Spell out numbers
Red [
Description: "Spell out numbers"
]
context [
digits: [One Two Three Four Five Six Seven Eight Nine]
tens: [_ Twenty Thirty Fourty Fifty Sixty Seventy Eighty Ninety]
teens: [Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen]
orders: [Thousand Million Billion]
out: copy []
res: copy []
set 'to-num-word function [num [integer!]][
clear head out
power: 0
if num < 0 [self/out: insert out 'Minus num: absolute num]
case [
num = 0 [append out 'Zero]
num < 10 [append out digits/:num]
num < 20 [append out teens/(num - 9)]
'else [
n: form num
while [m: take/part/last n 3][
clear res
if all [power > 0 0 < to-integer m] [
insert out pick orders power
]
forall m [
if m/1 <> #"0" [
switch length? m [
3 [append res pick digits to-integer form m/1 append res 'Hundred]
2 [
either m/1 = #"1" [
append res pick teens (to-integer m) - 9
][
d: form pick tens to-integer form m/1
if m/2 <> #"0" [
d: rejoin [d #"-" pick digits to-integer form m/2]
]
append res to-word d
]
m: next m
]
1 [append res pick digits to-integer form m/1]
]
]
]
insert out res
power: power + 1
]
]
]
head out
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment