Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created October 2, 2015 17:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ElemarJR/787f1a5c7da98cb909cd to your computer and use it in GitHub Desktop.
Save ElemarJR/787f1a5c7da98cb909cd to your computer and use it in GitHub Desktop.
let e numberStr =
match numberStr with
| "" -> numberStr
| _ -> numberStr + " e "
let writeNumber nr : string =
let unity = ["zero"; "um"; "dois"; "três"; "quatro"; "cinco"; "seis"; "sete"; "oito"; "nove"; "dez"; "onze"; "doze"; "treze"; "quatorze"; "quinze"; "dezesseis"; "dezessete"; "dezoito"; "dezenove"]
let decimals = [""; "cem"; "vinte"; "trinta"; "quarenta"; "cinquenta"; "sessenta"; "setenta"; "oitenta"; "noventa"]
let hundreds = [""; "cento"; "duzentos"; "trezentos"; "quatrocentos"; "quinhentos"; "seissentos"; "setecentos"; "oitocentos"; "novecentos"]
let thousand = "mil"
let mutable number = nr
let mutable numberStr = ""
let thousandNr = number / 1000
if(thousandNr <> 0) then
if(thousandNr = 1) then
numberStr <- thousand
else
numberStr <- unity.[thousandNr] + " " + thousand
number <- number - thousandNr * 1000
if(number <> 0 || numberStr = "") then
let hundredNr = number / 100
number <- number - hundredNr * 100
if(hundredNr <> 0) then
if(number = 0) then
numberStr <- (e numberStr) + decimals.[hundredNr]
else
if(numberStr <> "") then
numberStr <- numberStr + " "
numberStr <- numberStr + hundreds.[hundredNr]
if(number < 20) then
numberStr <- (e numberStr) + unity.[number]
else
let decimalNr = number / 10
number <- number - decimalNr * 10
if(number = 0) then
numberStr <- (e numberStr) + decimals.[decimalNr]
else
numberStr <- (e numberStr) + decimals.[decimalNr] + " e " + unity.[number]
numberStr
let writeNumber2 nr =
let unity = ["zero"; "um"; "dois"; "três"; "quatro"; "cinco"; "seis"; "sete"; "oito"; "nove"; "dez"; "onze"; "doze"; "treze"; "quatorze"; "quinze"; "dezesseis"; "dezessete"; "dezoito"; "dezenove"]
let decimals = [""; "cem"; "vinte"; "trinta"; "quarenta"; "cinquenta"; "sessenta"; "setenta"; "oitenta"; "noventa"]
let hundreds = [""; "cento"; "duzentos"; "trezentos"; "quatrocentos"; "quinhentos"; "seissentos"; "setecentos"; "oitocentos"; "novecentos"]
let thousand = "mil"
let fthousand number =
let thousandNr = number / 1000
let discounted = number - thousandNr * 1000
match thousandNr with
| 0 -> (number, "")
| 1 -> (discounted, thousand)
| _ -> (discounted, unity.[thousandNr] + " " + thousand)
let fhundred (number, prefix) =
let hundredNr = number / 100
let discounted = number - hundredNr * 100
match hundredNr with
| 0 -> (number, prefix)
| _ -> (discounted, prefix + (if prefix <> "" then " " else "") + hundreds.[hundredNr])
let inline e numberStr =
match numberStr with
| "" -> numberStr
| _ -> numberStr + " e "
let fdecimal (number, prefix) =
if number < 20
then (number, prefix)
else
let decimalNr = number / 10
let discounted = number - decimalNr * 10
(discounted, (e prefix) + decimals.[decimalNr])
let funit (number, prefix) =
if number = 0 && prefix <> ""
then prefix
else (e prefix) + unity.[number]
nr |> fthousand |> fhundred |> fdecimal |> funit
printf "Digite um número: \n"
let number = int32(System.Console.ReadLine())
let str = writeNumber2 number
printf "\n%s\n" str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment