Last active
August 29, 2015 14:03
-
-
Save cloudRoutine/16351f4a1ecf36307aa6 to your computer and use it in GitHub Desktop.
Function that takes a bigint as input and outputs its word form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let english_integer ( i : bigint ) : string = | |
let ZERO_TO_19 = | |
[ "zero" ; "one" ; "two" ; "three" ; "four" ; "five" ; | |
"six" ; "seven" ; "eight" ; "nine" ; "ten" ; "eleven" ; | |
"twelve" ; "thirteen" ; "fourteen" ; "fifteen" ; "sixteen" ; "seventeen" ; | |
"eighteen" ; "nineteen" ] | |
let HYPHEN_ONES = | |
[ "" ; "-one" ; "-two" ; "-three" ; "-four" ; "-five" ; | |
"-six" ; "-seven" ; "-eight" ; "-nine" ] | |
let TWENTY_UP = | |
[ "" ; "" ; "twenty" ; "thirty" ; "forty" ; | |
"fifty" ; "sixty" ; "seventy" ; "eighty" ; "ninety" ] | |
let THOUSAND_UP = | |
[ "" ; " thousand" ; " million" ; " billion" ; | |
" trillion" ; " quadrillion" ; " quintillion" ; " sextillion" ; | |
" septillion" ; " octillion" ; " nonillion" ; " decillion" ; | |
" undecillion" ; " duodecillion" ; " tredecillion" ; " quattuordecillion" ; | |
" sexdecillion" ; " septendecillion" ; " octodecillion" ; " novemdecillion" ; | |
" vigintillion" ] | |
let rec toEnglish i step = | |
match i with | |
| x when x < 0I -> "negative " + toEnglish -x 0 | |
| x when x < 20I -> ZERO_TO_19.Item (x|>int) + THOUSAND_UP.Item (step) | |
| x when x < 100I -> TWENTY_UP.Item (( x|>int ) / 10 ) + | |
HYPHEN_ONES.Item (( x|>int ) % 10 ) + | |
THOUSAND_UP.Item ( step ) | |
| x when x < 1000I -> let s = ( HYPHEN_ONES.Item (( x|> int) / 100 )).Substring 1 + " hundred" | |
if i % 100I > 0I | |
then s + " and " + toEnglish ( x % 100I ) 0 + THOUSAND_UP.Item ( step ) | |
else s + THOUSAND_UP.Item ( step ) | |
| x -> let current = i % 1000I | |
match current with | |
| x when x = 0I -> toEnglish (i/1000I) ( step + 1 ) | |
| x -> toEnglish (i/1000I) ( step + 1 ) + " " + | |
toEnglish current ( step ) | |
in toEnglish i 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment