-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathNumberUtils.kt
More file actions
139 lines (117 loc) · 3.54 KB
/
NumberUtils.kt
File metadata and controls
139 lines (117 loc) · 3.54 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package info.appdev.charting.utils
import timber.log.Timber
import kotlin.math.ceil
import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.roundToInt
/**
* Returns the appropriate number of decimals to be used for the provided number.
*/
fun Float.getDecimals(): Int {
val i = this.toDouble().roundToNextSignificant()
if (i.isInfinite()) {
return 0
}
return ceil(-log10(i)).toInt() + 2
}
/**
* rounds the given number to the next significant number
*/
fun Double.roundToNextSignificant(): Double {
if (this.isInfinite() ||
this.isNaN() || this == 0.0
) {
return 0.0
}
val d = ceil(log10(if (this < 0) -this else this).toFloat().toDouble())
val pw = 1 - d.toInt()
val magnitude = 10.0.pow(pw.toDouble())
val shifted = (this * magnitude).roundToInt()
return shifted / magnitude
}
/**
* This method converts dp unit to equivalent pixels, depending on device
* density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.
*
* dp A value in dp (density independent pixels) unit. Which we need
* to convert into pixels
* @return A float value to represent px equivalent to dp depending on
* device density
*/
fun Float.convertDpToPixel(): Float {
if (metrics == null) {
Timber.e("Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...). Otherwise conversion does not take place.")
return this
} else
return this * metrics!!.density
}
fun Float.formatNumber(digitCount: Int, separateThousands: Boolean, separateChar: Char = '.'): String {
var number = this
var digitCount = digitCount
val out = CharArray(35)
var neg = false
if (number == 0f) {
return "0"
}
val zero = number < 1 && number > -1
if (number < 0) {
neg = true
number = -number
}
if (digitCount > Utils.POW_10.size) {
digitCount = Utils.POW_10.size - 1
}
number *= Utils.POW_10[digitCount].toFloat()
var lVal = Math.round(number).toLong()
var ind = out.size - 1
var charCount = 0
var decimalPointAdded = false
while (lVal != 0L || charCount < (digitCount + 1)) {
val digit = (lVal % 10).toInt()
lVal /= 10
out[ind--] = (digit + '0'.code).toChar()
charCount++
// add decimal point
if (charCount == digitCount) {
out[ind--] = ','
charCount++
decimalPointAdded = true
// add thousand separators
} else if (separateThousands && lVal != 0L && charCount > digitCount) {
if (decimalPointAdded) {
if ((charCount - digitCount) % 4 == 0) {
out[ind--] = separateChar
charCount++
}
} else {
if ((charCount - digitCount) % 4 == 3) {
out[ind--] = separateChar
charCount++
}
}
}
}
// if number around zero (between 1 and -1)
if (zero) {
out[ind--] = '0'
charCount += 1
}
// if the number is negative
if (neg) {
out[ind--] = '-'
charCount += 1
}
val start = out.size - charCount
// use this instead of "new String(...)" because of issue < Android 4.0
return String(out, start, out.size - start)
}
/**
* returns an angle between 0.f < 360.f (not less than zero, less than 360)
*/
fun Float.getNormalizedAngle(): Float {
var angle = this
while (angle < 0f) {
angle += 360f
}
return angle % 360f
}