Skip to content

Commit 4ad7933

Browse files
committed
Fix spelled out function for negatives in English! v1.2.2
Negative numbers were not spelled out properly for small numbers or for rationals. This version fixes these errors. See merge request #60
1 parent c210254 commit 4ad7933

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SpelledOut"
22
uuid = "4728c690-e668-4265-bc0d-51a8c0f93067"
33
authors = ["Jake W. Ireland <[email protected]> and contributors"]
4-
version = "1.2.1"
4+
version = "1.2.2"
55

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

src/en.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ function _spelled_out_en!(io::IOBuffer, number_norm::Integer; british::Bool = fa
8484
end
8585

8686
if number_norm < 0
87+
# In "Merriam-Webster's Guide to Everyday Math: A Home and Business Reference"
88+
# by Brian Burrell (1998), the adjective "negative" over "minus" is used for
89+
# negative numbers.
90+
#
91+
# See #40: SpelledOut.jl/issues/40
8792
print(io, "negative ")
93+
number_norm = abs(number_norm)
8894
end
8995

9096
if number_norm > limit - 1
@@ -223,7 +229,7 @@ function spelled_out_en(number::Rational; british::Bool = false, dict::Symbol =
223229
word = spelled_out_en(_num, british = british, dict = dict) * " " * spell_ordinal_en(_den, british = british, dict = dict)
224230

225231
# account for pluralisation
226-
return isone(_num) ? word : word * "s"
232+
return isone(abs(_num)) ? word : word * "s"
227233
end
228234

229235
function spelled_out_en(number::AbstractIrrational; british::Bool = false, dict::Symbol = :modern)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ using SpelledOut
2222
@test spelled_out(2^log2(3390000), lang = :en) == "three million, three hundred ninety thousand" # should convert 3.3899999999999986e6 to an integer
2323
@test spelled_out(1000000.01, lang = :en) == "one million point zero one" # parse big floats correctly (i.e., avoid scientific notation. Caveat: this is slow)
2424
@test spelled_out(1//3, lang = :en_UK) == "one third"
25+
@test spelled_out(2//3, lang = :en_UK) == "two thirds"
2526
@test spelled_out(1//5, lang = :en_UK) == "one fifth"
27+
@test spelled_out(2//5, lang = :en_UK) == "two fifths"
2628
@test spelled_out(1//102, lang = :en_UK) == "one one hundred and second"
2729
@test spelled_out(1//102, lang = :en_US) == "one one hundred second"
2830
@test spelled_out(5//102, lang = :en_UK) == "five one hundred and seconds"
@@ -31,6 +33,18 @@ using SpelledOut
3133
@test spelled_out(40//1, lang = :en_UK) == "forty"
3234
@test spelled_out(40//2, lang = :en_UK) == "twenty"
3335
@test spelled_out(40//6, lang = :en_UK) == "twenty thirds"
36+
37+
@test spelled_out(-1; lang=:en_UK) == "negative one"
38+
@test spelled_out(-19; lang=:en_UK) == "negative nineteen"
39+
@test spelled_out(-198; lang=:en_UK) == "negative one hundred and ninety-eight"
40+
@test spelled_out(-1982; lang=:en_UK) == "negative one thousand, nine hundred and eighty-two"
41+
@test spelled_out(-3.0; lang=:en_UK) == "negative three"
42+
@test spelled_out(-1//3; lang=:en_UK) == "negative one third"
43+
@test spelled_out(-(1//3); lang=:en_UK) == "negative one third"
44+
@test spelled_out(-3//1; lang=:en_UK) == "negative three"
45+
@test spelled_out(-(3//1); lang=:en_UK) == "negative three"
46+
@test spelled_out(3//-1; lang=:en_UK) == "negative three"
47+
@test spelled_out(Complex(-2, -3); lang=:en_UK) == "negative two and negative three imaginaries"
3448
end
3549

3650
@testset "Spanish" begin

0 commit comments

Comments
 (0)