Skip to content

Commit ab9c1ec

Browse files
authored
Initial release with test (#1)
* Finalised user API for initial release
1 parent d5da1af commit ab9c1ec

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

.github/workflows/CI.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
- windows-latest
2020
arch:
2121
- x64
22-
- x86
2322
exclude:
2423
- os: macOS-latest
2524
arch: x86

src/InstaRound.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
module InstaRound
22

33
# Write your package code here.
4+
import Base: round
5+
6+
7+
include("custom.jl")
8+
include("utils.jl")
9+
10+
11+
####### Globals Exports #######
12+
export round, IGRound
13+
414

515
end

src/custom.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
IGRound
3+
4+
Abstract base type for dispatched InstaRound rounds.
5+
"""
6+
abstract type IGRound end

src/utils.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
business_round(num)
3+
4+
Internal function to round a number to the nearest IG style.
5+
"""
6+
function business_round(num)
7+
if num < 10^3
8+
return string(num)
9+
elseif num < 10^4
10+
return string(string(num)[1], "K")
11+
elseif num < 10^5
12+
return string(string(num)[1:2], "K")
13+
elseif num < 10^6
14+
return string(string(num)[1:3], "K")
15+
elseif num < 10^7
16+
return string(string(num)[1], "M")
17+
elseif num < 10^8
18+
return string(string(num)[1:2], "M")
19+
elseif num < 10^9
20+
return string(string(num)[1:3], "M")
21+
elseif num < 10^10
22+
return string(string(num)[1], "B")
23+
elseif num < 10^11
24+
return string(string(num)[1:2], "B")
25+
elseif num < 10^12
26+
return string(string(num)[1:3], "B")
27+
elseif num < 10^13
28+
return string(string(num)[1], "T")
29+
elseif num < 10^14
30+
return string(string(num)[1:2], "T")
31+
elseif num < 10^15
32+
return string(string(num)[1:3], "T")
33+
elseif num < 10^16
34+
return string(string(num)[1], "Q")
35+
elseif num < 10^17
36+
return string(string(num)[1:2], "Q")
37+
elseif num < 10^18
38+
return string(string(num)[1:3], "Q")
39+
end
40+
end
41+
42+
43+
"""
44+
round(::Type{IGRound}, x::Number)
45+
46+
Main function to round a number x to the nearest IG style.
47+
48+
# Arguments
49+
* x: The number to round.
50+
51+
```
52+
# Example
53+
julia> round(IGRound, 100_000)
54+
"100K"
55+
```
56+
"""
57+
function round(::Type{IGRound}, x::Number)
58+
init_round = Base.round(Int, x)
59+
return init_round |> business_round
60+
end

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,20 @@ using InstaRound
22
using Test
33

44
@testset "InstaRound.jl" begin
5-
# Write your tests here.
5+
@test round(IGRound, 999) == "999"
6+
@test round(IGRound, 1_000) == "1K"
7+
@test round(IGRound, 10_000) == "10K"
8+
@test round(IGRound, 100_000) == "100K"
9+
@test round(IGRound, 1_000_000) == "1M"
10+
@test round(IGRound, 10_000_000) == "10M"
11+
@test round(IGRound, 100_000_000) == "100M"
12+
@test round(IGRound, 1_000_000_000) == "1B"
13+
@test round(IGRound, 10_000_000_000) == "10B"
14+
@test round(IGRound, 100_000_000_000) == "100B"
15+
@test round(IGRound, 1_000_000_000_000) == "1T"
16+
@test round(IGRound, 10_000_000_000_000) == "10T"
17+
@test round(IGRound, 100_000_000_000_000) == "100T"
18+
@test round(IGRound, 1_000_000_000_000_000) == "1Q"
19+
@test round(IGRound, 10_000_000_000_000_000) == "10Q"
20+
@test round(IGRound, 100_000_000_000_000_000) == "100Q"
621
end

0 commit comments

Comments
 (0)