Skip to content

Commit

Permalink
Add helper function to print numbers as ordinal numerals (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Aug 20, 2023
1 parent 060bf95 commit 6651a34
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export NotImplementedError
export NotInvertibleError
export NumField
export NumFieldElem
export ordinal_number_string
export parent_type
export pluralize
export PolyRing
Expand Down Expand Up @@ -292,7 +293,7 @@ export ZZ

include("Attributes.jl")
include("AliasMacro.jl")
include("Pluralize.jl")
include("PrintHelper.jl")

# alternative names for some functions from Base
export is_empty, is_even, is_equal, is_finite, is_inf, is_integer, is_less, is_odd, is_one, is_real, is_subset, is_valid, is_zero
Expand Down
37 changes: 37 additions & 0 deletions src/Pluralize.jl → src/PrintHelper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,40 @@ function Base.show(io::IO, quantity::ItemQuantity)
print(io, pluralize(quantity.noun))
end
end

"""
ordinal_number_string(number::Int)
A helper function which returns `number` as a string in ordinal form.
# Examples
```julia
julia> ordinal_number_string(1)
"1st"
julia> ordinal_number_string(2)
"2nd"
julia> ordinal_number_string(3)
"3rd"
julia> ordinal_number_string(4)
"4rd"
```
"""
function ordinal_number_string(number::Int)
number >= 0 || error("number must be non-negative")
mod100 = mod(number, 100)
mod10 = mod(number, 10)
if mod100 in 11:13
return "$(number)th"
elseif mod10 == 1
return "$(number)st"
elseif mod10 == 2
return "$(number)nd"
elseif mod10 == 3
return "$(number)rd"
else
return "$(number)th"
end
end
2 changes: 1 addition & 1 deletion test/AbstractAlgebra-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ include("Maps-test.jl")
include("Benchmark-test.jl")
include("Factor-test.jl")
include("PrettyPrinting-test.jl")
include("Pluralize-test.jl")
include("PrintHelper-test.jl")
include("misc-test.jl")
24 changes: 0 additions & 24 deletions test/Pluralize-test.jl

This file was deleted.

46 changes: 46 additions & 0 deletions test/PrintHelper-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@testset "pluralize" begin
@test pluralize("basis") == "bases"
@test pluralize("child") == "children"
@test pluralize("generator") == "generators"
@test pluralize("indeterminate") == "indeterminates"
@test pluralize("matrix") == "matrices"
@test pluralize("maximum") == "maxima"
@test pluralize("polyhedron") == "polyhedra"
@test pluralize("tetrahedron") == "tetrahedra"
@test pluralize("variable") == "variables"
@test pluralize("variety") == "varieties"
@test pluralize("vertex") == "vertices"
end

@testset "ItemQuantity" begin
@test string(ItemQuantity(0, "generator")) == "0 generators"
@test string(ItemQuantity(1, "generator")) == "1 generator"
@test string(ItemQuantity(2, "generator")) == "2 generators"
@test string(ItemQuantity(0, "ox", "oxen")) == "0 oxen"
@test string(ItemQuantity(1, "ox", "oxen")) == "1 ox"
@test string(ItemQuantity(2, "ox", "oxen")) == "2 oxen"
end

@testset "ordinal_number_string" begin
@test ordinal_number_string(0) == "0th"
@test ordinal_number_string(1) == "1st"
@test ordinal_number_string(2) == "2nd"
@test ordinal_number_string(3) == "3rd"
@test ordinal_number_string(4) == "4th"
@test ordinal_number_string(5) == "5th"
@test ordinal_number_string(10) == "10th"
@test ordinal_number_string(11) == "11th"
@test ordinal_number_string(12) == "12th"
@test ordinal_number_string(13) == "13th"
@test ordinal_number_string(14) == "14th"
@test ordinal_number_string(20) == "20th"
@test ordinal_number_string(21) == "21st"
@test ordinal_number_string(22) == "22nd"
@test ordinal_number_string(23) == "23rd"
@test ordinal_number_string(24) == "24th"
@test ordinal_number_string(100) == "100th"
@test ordinal_number_string(101) == "101st"
@test ordinal_number_string(102) == "102nd"
@test ordinal_number_string(103) == "103rd"
@test ordinal_number_string(104) == "104th"
end

0 comments on commit 6651a34

Please sign in to comment.