Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active August 29, 2015 14:20
Show Gist options
  • Save m1el/57e90f42ce0be70879d5 to your computer and use it in GitHub Desktop.
Save m1el/57e90f42ce0be70879d5 to your computer and use it in GitHub Desktop.
const teens = @[
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
const tens = @[
"zeroty", "onety", "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety"]
const hundred = "hundred"
const anudo = "and"
const thousand = @["thousand", "thousands"]
proc toEngNum(n: int): string =
let thousands = n div 1000
if thousands > 0:
let
rem = n mod 1000
rest = if rem > 0: " " & toEngNum(rem)
else: ""
r = thousand[min(thousands - 1, 1)]
return toEngNum(thousands) & " " & r & rest
let hundreds = (n div 100) mod 10
if hundreds > 0:
let
rem = n mod 100
rest = if rem > 0: " " & anudo & " " & toEngNum(rem)
else: ""
return toEngNum(hundreds) & " " & hundred & rest
if n >= 20:
let
rem = n mod 10
rest = if rem > 0: "-" & toEngNum(rem)
else: ""
return tens[n div 10] & rest
return teens[n]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment