Skip to content

Commit 40bc98a

Browse files
authored
Stable API Release (#4)
* Initial Release
1 parent ab9c1ec commit 40bc98a

File tree

6 files changed

+338
-67
lines changed

6 files changed

+338
-67
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,34 @@
44
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://PyDataBlog.github.io/InstaRound.jl/dev)
55
[![Build Status](https://github.com/PyDataBlog/InstaRound.jl/workflows/CI/badge.svg)](https://github.com/PyDataBlog/InstaRound.jl/actions)
66
[![Coverage](https://codecov.io/gh/PyDataBlog/InstaRound.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/PyDataBlog/InstaRound.jl)
7+
8+
A simple package for extending Base.round with instagram-like rounding.
9+
10+
## Installation
11+
12+
To install `InstaRound` either do
13+
14+
```julia
15+
using Pkg
16+
Pkg.add("InstaRound")
17+
```
18+
19+
or switch to `Pkg` mode with `]` and issue
20+
21+
```julia
22+
pkg> add InstaRound
23+
```
24+
25+
## Basic Usage
26+
27+
Round numbers with IGRound
28+
29+
```julia
30+
using InstaRound
31+
32+
julia> round(IGRound, 1_000_000; names=false)
33+
"1M"
34+
35+
julia> round(IGRound, 1_000_000; names=true)
36+
"1Million"
37+
```

docs/src/index.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,86 @@ CurrentModule = InstaRound
66

77
Documentation for [InstaRound](https://github.com/PyDataBlog/InstaRound.jl).
88

9+
A simple package for extending Base.round with instagram like rounding.
10+
11+
## Installation
12+
13+
To install `InstaRound` either do
14+
15+
```julia
16+
using Pkg
17+
Pkg.add("InstaRound")
18+
```
19+
20+
or switch to `Pkg` mode with `]` and issue
21+
22+
```julia
23+
pkg> add InstaRound
24+
25+
## Basic Usage
26+
Round numbers with IGRound
27+
28+
```julia
29+
using InstaRound
30+
31+
julia> round(IGRound, 1_000_000; names=false)
32+
"1M"
33+
```
34+
35+
## Practical Use Case
36+
37+
```julia
38+
using InstaRound
39+
using MarketData
40+
using Dates
41+
42+
43+
44+
start = DateTime(2017, 1, 1)
45+
df = yahoo(:AMZN, YahooOpt(period1 = start))
46+
```
47+
48+
```julia
49+
julia> round.(IGRound, df.AdjClose; names=false)
50+
1137×1 TimeArray{String, 1, Date, Vector{String}} 2017-01-03 to 2021-07-09
51+
│ │ AdjClose │
52+
├────────────┼──────────┤
53+
│ 2017-01-03 │ "754" │
54+
│ 2017-01-04 │ "757" │
55+
│ 2017-01-05 │ "780" │
56+
│ ⋮ │ ⋮ │
57+
│ 2021-07-07 │ "3K" │
58+
│ 2021-07-08 │ "3K" │
59+
│ 2021-07-09 │ "3K" │
60+
```
61+
62+
## Abbreviation Source
63+
64+
|Abbreviation|Name |Value|Equivalent|
65+
|:----------:|:---------------:|:---:|:--------:|
66+
| K | Thousand (Kilo) |10^3 | 1000 |
67+
| M | Million |10^6 | 1000K |
68+
| B | Billion |10^9 | 1000M |
69+
| t | Trillion |10^12| 1000B |
70+
| q | Quadrillion |10^15| 1000t |
71+
| Q | Quintillion |10^18| 1000q |
72+
| s | Sextillion |10^21| 1000Q |
73+
| S | Septillion |10^24| 1000s |
74+
| o | Octillion |10^27| 1000S |
75+
| n | Nonillion |10^30| 1000o |
76+
| d | Decillion |10^33| 1000n |
77+
| U | Undecillion |10^36| 1000d |
78+
| D | Duodecillion |10^39| 1000U |
79+
| T | Tredecillion |10^42| 1000D |
80+
| Qt |Quattuordecillion|10^45| 1000T |
81+
| Qd | Quindecillion |10^48| 1000Qt |
82+
| Sd | Sexdecillion |10^51| 1000Qd |
83+
| St | Septendecillion |10^54| 1000Sd |
84+
| O | Octodecillion |10^57| 1000St |
85+
| N | Novemdecillion |10^60| 1000O |
86+
| v | Vigintillion |10^63| 1000N |
87+
| c | Unvigintillion |10^66| 1000v |
88+
989
```@index
1090
```
1191

src/InstaRound.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module InstaRound
22

3-
# Write your package code here.
3+
# Import Base round
44
import Base: round
55

66

@@ -9,7 +9,7 @@ include("utils.jl")
99

1010

1111
####### Globals Exports #######
12-
export round, IGRound
12+
export IGRound
1313

1414

1515
end

src/custom.jl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,37 @@
33
44
Abstract base type for dispatched InstaRound rounds.
55
"""
6-
abstract type IGRound end
6+
abstract type IGRound end
7+
8+
9+
units = [
10+
"K", "M", "B", "t", "q", "Q", "s", "S", "o",
11+
"n", "d", "U", "D", "T", "Qt", "Qd", "Sd", "St",
12+
"O", "N", "v", "c"
13+
]
14+
15+
16+
unit_names = [
17+
"Thousand",
18+
"Million",
19+
"Billion",
20+
"Trillion",
21+
"Quadrillion",
22+
"Quintillion",
23+
"Sextillion",
24+
"Septillion",
25+
"Octillion",
26+
"Nonillion",
27+
"Decillion",
28+
"Undecillion",
29+
"Duodecillion",
30+
"Tredecillion",
31+
"Quattuordecillion",
32+
"Quindecillion",
33+
"Sexdecillion",
34+
"Septendecillion",
35+
"Octodecillion",
36+
"Novemdecillion",
37+
"Vigintillion",
38+
"Unvigintillion",
39+
]

src/utils.jl

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
11
"""
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")
2+
log_transformer(x)
3+
4+
Internal function used to get the exponent of x.
5+
Used to select the nearest unit index.
6+
"""
7+
function log_transformer(x)
8+
return floor(Int, log10(x))
9+
end
10+
11+
12+
"""
13+
business_round(number::Number, names::Bool)
14+
15+
Internal function to round a number (num) to the nearest IG style.
16+
"""
17+
function business_round(number::Number, names::Bool)
18+
if number < 1000
19+
return string(number)
20+
else
21+
return extract_identifying_unit(number, names)
22+
end
23+
end
24+
25+
26+
"""
27+
extract_identifying_unit(num::Number, names::Bool)
28+
29+
Internal function to extract the identifying unit from a number.
30+
"""
31+
function extract_identifying_unit(num::Number, names::Bool)
32+
log_pos = log_transformer(num)
33+
div_3 = log_pos ÷ 3
34+
35+
if names
36+
unit = unit_names[div_3]
37+
else
38+
unit = units[div_3]
3939
end
40+
identifier = num / ( BigInt(10) ^ (3 * div_3) ) |> x -> string(floor(Int, x))
41+
42+
return string(identifier, unit)
4043
end
4144

4245

4346
"""
44-
round(::Type{IGRound}, x::Number)
47+
round(::Type{IGRound}, x::Number; names::Bool=false)
4548
46-
Main function to round a number x to the nearest IG style.
49+
Function to round a number x to the nearest IG style.
4750
4851
# Arguments
49-
* x: The number to round.
52+
* x: The number to be rounded.
53+
* names: If true, the unit will be names, if false, the unit will be the number.
5054
51-
```
5255
# Example
53-
julia> round(IGRound, 100_000)
54-
"100K"
56+
57+
```julia-repl
58+
julia> round(IGRound, 100_000; names=true)
59+
"100Thousand"
5560
```
5661
"""
57-
function round(::Type{IGRound}, x::Number)
58-
init_round = Base.round(Int, x)
59-
return init_round |> business_round
60-
end
62+
function round(::Type{IGRound}, x::Number; names::Bool=false)
63+
init_round = Base.round(BigInt, x)
64+
return business_round(init_round, names)
65+
end

0 commit comments

Comments
 (0)