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)
4043end
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