Skip to content

Commit 3eb3f6d

Browse files
authored
Add decimals to roundings (#6)
* Add decimals to rounding
1 parent 40bc98a commit 3eb3f6d

File tree

6 files changed

+215
-157
lines changed

6 files changed

+215
-157
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13-
- '1.0'
13+
- '1.1'
1414
- '1.6'
1515
- 'nightly'
1616
os:

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "InstaRound"
22
uuid = "609f54d3-8a97-4836-aa37-e3dc8f03ac88"
3-
authors = ["PyDataBlog <pimpfada@gmail.com> and contributors"]
4-
version = "0.1.0"
3+
authors = ["PyDataBlog <pimpfada@gmail.com>"]
4+
version = "0.2.0"
55

66
[compat]
77
julia = "1"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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)
77

8-
A simple package for extending Base.round with instagram-like rounding.
8+
A simple package for extending Base.round with a more human readable rounding style.
99

1010
## Installation
1111

@@ -30,8 +30,8 @@ Round numbers with IGRound
3030
using InstaRound
3131

3232
julia> round(IGRound, 1_000_000; names=false)
33-
"1M"
33+
"1.0M"
3434

3535
julia> round(IGRound, 1_000_000; names=true)
36-
"1Million"
36+
"1.0 Million"
3737
```

docs/src/index.md

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,73 @@ or switch to `Pkg` mode with `]` and issue
2121

2222
```julia
2323
pkg> add InstaRound
24+
```
2425

2526
## Basic Usage
27+
2628
Round numbers with IGRound
2729

2830
```julia
2931
using InstaRound
3032

3133
julia> round(IGRound, 1_000_000; names=false)
32-
"1M"
34+
"1.0M"
35+
36+
julia> round(IGRound, 1_000_000; names=true)
37+
"1.0 Million"
3338
```
3439

3540
## Practical Use Case
3641

3742
```julia
3843
using InstaRound
39-
using MarketData
40-
using Dates
44+
using PolygonIO
45+
using DataFrames
4146

4247

48+
opts = PolyOpts(API_KEY, DataFrame)
49+
bars_df = crypto_aggregates_bars(opts, "X:BTCUSD", 5, "minute", "2020-10-14", "2020-10-16")
50+
```
4351

44-
start = DateTime(2017, 1, 1)
45-
df = yahoo(:AMZN, YahooOpt(period1 = start))
52+
```julia-repl
53+
julia> round.(IGRound, bars_df.c; names=false)
54+
24-element Vector{String}:
55+
"11.42K"
56+
"11.42K"
57+
"11.44K"
58+
"11.45K"
59+
"11.45K"
60+
"11.45K"
61+
"11.45K"
62+
63+
"11.44K"
64+
"11.44K"
65+
"11.44K"
66+
"11.45K"
67+
"11.45K"
68+
"11.45K"
69+
"11.44K"
4670
```
4771

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" │
72+
```julia-repl
73+
julia> round.(IGRound, bars_df.c; names=true)
74+
24-element Vector{String}:
75+
"11.42 Thousand"
76+
"11.42 Thousand"
77+
"11.44 Thousand"
78+
"11.45 Thousand"
79+
"11.45 Thousand"
80+
"11.45 Thousand"
81+
"11.45 Thousand"
82+
83+
"11.44 Thousand"
84+
"11.44 Thousand"
85+
"11.44 Thousand"
86+
"11.45 Thousand"
87+
"11.45 Thousand"
88+
"11.45 Thousand"
89+
"11.44 Thousand"
90+
6091
```
6192

6293
## Abbreviation Source

src/utils.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ function business_round(number::Number, names::Bool)
2323
end
2424

2525

26+
"""
27+
number_groups(s::AbstractString)
28+
29+
Internal function to extract the two groups of numbers from a '.' separated string.
30+
"""
31+
function number_groups(s::AbstractString)
32+
group_1 = match(r"(\d+)", s)[1]
33+
group_2 = match(r"(\d+$)", s)[1]
34+
35+
if length(group_2) > 1
36+
group_2 = group_2[1:2]
37+
else
38+
group_2 = group_2[1]
39+
end
40+
return group_1, group_2
41+
end
42+
2643
"""
2744
extract_identifying_unit(num::Number, names::Bool)
2845
@@ -37,9 +54,15 @@ function extract_identifying_unit(num::Number, names::Bool)
3754
else
3855
unit = units[div_3]
3956
end
40-
identifier = num / ( BigInt(10) ^ (3 * div_3) ) |> x -> string(floor(Int, x))
4157

42-
return string(identifier, unit)
58+
indentifier = string(num / ( BigInt(10) ^ (3 * div_3) ))
59+
main_int, rem_float = number_groups(indentifier)
60+
61+
if names
62+
return string(main_int, ".", rem_float, " ", unit)
63+
else
64+
return string(main_int, ".", rem_float, unit)
65+
end
4366
end
4467

4568

0 commit comments

Comments
 (0)