Skip to content

Commit

Permalink
Merge pull request #16 from jakewilliami/dev
Browse files Browse the repository at this point in the history
Implemented floating point spelled out (closes #14)
  • Loading branch information
jakewilliami authored Nov 1, 2020
2 parents 75cb64f + 46ab4d7 commit 82f42ce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ uuid = "4728c690-e668-4265-bc0d-51a8c0f93067"
authors = ["Jake W. Ireland <[email protected]> and contributors"]
version = "1.0.1"

[deps]
DecFP = "55939f99-70c6-5e9b-8bb0-5071ed7d61fd"

[compat]
julia = "1"

Expand Down
38 changes: 24 additions & 14 deletions src/SpelledOut.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module SpelledOut

using DecFP: Dec64

export spelled_out, Spelled_out, Spelled_Out, SPELLED_OUT

include(joinpath(dirname(@__FILE__), "standard_dictionary_numbers_extended.jl"))
include(joinpath(dirname(@__FILE__), "large_standard_dictionary_numbers_extended.jl"))

# convert a value < 100 to English.
function small_convert(number::Integer; british::Bool=false, dict::Symbol=:modern)::String
function small_convert(number::Integer; british::Bool=false, dict::Symbol=:modern)
scale_numbers = _scale_modern # define scale type
if number < 20
word = _small_numbers[number + 1]
Expand Down Expand Up @@ -35,7 +37,7 @@ end
# convert a value < 1000 to english, special cased because it is the level that excludes
# the < 100 special case. The rest are more general. This also allows you to get
# strings in the form of "forty-five hundred" if called directly.
function large_convert(number::Integer; british::Bool=false, dict::Symbol=:modern)::String
function large_convert(number::Integer; british::Bool=false, dict::Symbol=:modern)
scale_numbers = _scale_modern # define scale type
word = string() # initiate empty string
divisor = div(number, 100)
Expand All @@ -61,7 +63,18 @@ function large_convert(number::Integer; british::Bool=false, dict::Symbol=:moder
return word
end

function spelled_out(number::Integer; british::Bool=false, dict::Symbol=:modern)::String
function decimal_convert(number::AbstractFloat; british::Bool=false, dict::Symbol=:modern)
whole, decimal = split(string(number), ".")
word = spelled_out(parse(BigInt, whole), british=british, dict=dict) * string(" point")

for i in decimal
word = word * " " * _small_number_dictionary[i]
end

return word
end

function spelled_out(number::Integer; british::Bool=false, dict::Symbol=:modern)
scale_numbers = _scale_modern # default to :modern
if isequal(dict, :british)
scale_numbers = _scale_traditional_british
Expand Down Expand Up @@ -125,21 +138,18 @@ function spelled_out(number::Integer; british::Bool=false, dict::Symbol=:modern)
end
end

function spelled_out(number::AbstractFloat; british::Bool=false, dict::Symbol=:modern)::String
type = typeof(number)
number = big(number)

function spelled_out(number::AbstractFloat; british::Bool=false, dict::Symbol=:modern)
try
number = BigInt(number)
return spelled_out(BigInt(number), british=british, dict=dict)
catch
throw(error("Cannot parse an object of type `$(type)` into the required integer type."))
number = Dec64(number)

return decimal_convert(number, british=british, dict=dict)
end

return spelled_out(number, british=british, dict=dict)
end

Spelled_out(number::Real; british::Bool=false, dict::Symbol=:modern)::String = uppercasefirst(spelled_out(number, british=british, dict=dict))
Spelled_Out(number::Real; british::Bool=false, dict::Symbol=:modern)::String = titlecase(spelled_out(number, british=british, dict=dict))
SPELLED_OUT(number::Real; british::Bool=false, dict::Symbol=:modern)::String = uppercase(spelled_out(number, british=british, dict=dict))
Spelled_out(number::Number; british::Bool=false, dict::Symbol=:modern) = uppercasefirst(spelled_out(number, british=british, dict=dict))
Spelled_Out(number::Number; british::Bool=false, dict::Symbol=:modern) = titlecase(spelled_out(number, british=british, dict=dict))
SPELLED_OUT(number::Number; british::Bool=false, dict::Symbol=:modern) = uppercase(spelled_out(number, british=british, dict=dict))

end # end module
4 changes: 4 additions & 0 deletions src/standard_dictionary_numbers_extended.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const _small_numbers = String[
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]

const _small_number_dictionary = Dict{Char, String}(
'0' => "zero", '1' => "one", '2' => "two", '3' => "three", '4' => "four", '5' => "five", '6' => "six",
'7' => "seven", '8' => "eight", '9' => "nine")

const _tens = String[
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ using Test
@test spelled_out(123456789, british=true, dict=:british) == "one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine"
@test spelled_out(1234567890123, british=true, dict=:british) == "one billion, two hundred and thirty-four thousand million, five hundred and sixty-seven million, eight hundred and ninety thousand, one hundred and twenty-three"
@test spelled_out(1234567890123, british=true, dict=:modern) == "one trillion, two hundred and thirty-four billion, five hundred and sixty-seven million, eight hundred and ninety thousand, one hundred and twenty-three"
@test spelled_out(3.0) == "three"
@test spelled_out(3) == "three"
@test spelled_out(3.141592653) == "three point one four one five nine two six five three"
end

0 comments on commit 82f42ce

Please sign in to comment.