/
ordinal.jl
57 lines (44 loc) · 1.07 KB
/
ordinal.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This file is a part of AstroLib.jl. License is MIT "Expat".
"""
ordinal(num) -> result
### Purpose ###
Convert an integer to a correct English ordinal string.
### Explanation ###
The first four ordinal strings are "1st", "2nd", "3rd", "4th" ....
### Arguments ###
* `num`: number to be made ordinal. It should be of type int.
### Output ###
* `result`: ordinal string, such as '1st' '3rd '164th' '87th' etc
### Example ###
```jldoctest
julia> using AstroLib
julia> ordinal.(1:5)
5-element Array{String,1}:
"1st"
"2nd"
"3rd"
"4th"
"5th"
```
### Notes ###
This function does not support float arguments, unlike the IDL implementation.
Code of this function is based on IDL Astronomy User's Library.
"""
function ordinal(num::Integer)
a = num % 100
if a== 11 || a == 12 || a == 13
suffix = "th"
else
a = num % 10
if a == 1
suffix = "st"
elseif a == 2
suffix = "nd"
elseif a == 3
suffix = "rd"
else
suffix = "th"
end
end
return string(num) * suffix
end