-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
56 lines (53 loc) · 1.42 KB
/
utils.go
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
package myzo
import (
"log"
"strconv"
)
// So this function is because Monzo stores balances, etc as a 64 bit integer.
// With that being said, this function makes it more friendly to read.
func Convert64IntToFloat(v int64) float64 {
before := v
if v < 0 {
v = (v - v) - v
}
lastTwoDigits := v % 100
allDigitsExceptLast := v / 100
var lastTwoDigitsStr string
lastTwoDigitsStr = strconv.Itoa(int(lastTwoDigits))
if lastTwoDigits < 10 {
lastTwoDigitsStr = "0" + lastTwoDigitsStr
}
allDigitsExceptLastStr := strconv.Itoa(int(allDigitsExceptLast))
floatingPointNumber := allDigitsExceptLastStr + "." + lastTwoDigitsStr
num, err := strconv.ParseFloat(floatingPointNumber, 64)
if err != nil {
log.Println("Could not convert to float!")
return 0
}
if before > 0 {
return num
} else {
return -num
}
}
// String version so you can make the decimal places have to in case of it ending in 0
func Convert64IntToString(v int64) string {
before := v
if v < 0 {
v = (v - v) - v
}
lastTwoDigits := v % 100
allDigitsExceptLast := v / 100
var lastTwoDigitsStr string
lastTwoDigitsStr = strconv.Itoa(int(lastTwoDigits))
if lastTwoDigits < 10 {
lastTwoDigitsStr = "0" + lastTwoDigitsStr
}
allDigitsExceptLastStr := strconv.Itoa(int(allDigitsExceptLast))
floatingPointNumber := allDigitsExceptLastStr + "." + lastTwoDigitsStr
if before > 0 {
return floatingPointNumber
} else {
return "-" + floatingPointNumber
}
}