-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfloat.go
134 lines (119 loc) · 2.79 KB
/
float.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
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
package goutils
import (
"fmt"
"math/big"
"strconv"
"strings"
)
/*
Subtract
*/
func Subtract(valo1, valor2 float64) float64 {
const prec = 200
a := new(big.Float).SetPrec(prec).SetFloat64(valo1)
b := new(big.Float).SetPrec(prec).SetFloat64(valor2)
result := new(big.Float).Sub(a, b)
retorno, _ := strconv.ParseFloat(result.String(), 64)
return retorno
}
/*
ConvertFloatToFloatScale2
*/
func ConvertFloatToFloatScale2(valor float64) float64 {
value := strconv.FormatFloat(valor, 'f', 2, 64)
s, _ := strconv.ParseFloat(value, 64)
return s
}
/*
ConvertFloat64ToString
*/
func ConvertFloat64ToString(value float64) string {
s := fmt.Sprintf("%.2f", value)
return s
}
/*
ConvertkeepZeroToFloat64
*/
func ConvertkeepZeroToFloat64(value KeepZero) float64 {
s, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
return s
}
func ConvertkeepZeroToFloat64To4Decimal(value KeepZero) float64 {
s, _ := strconv.ParseFloat(fmt.Sprintf("%.4f", value), 64)
return s
}
/*
ConvertFloat64ToString4Decimal
*/
func ConvertFloat64ToString4Decimal(value float64) string {
s := fmt.Sprintf("%.4f", value)
return s
}
/*
ConvertStringToFloat64
*/
func ConvertStringToFloat64(value string) float64 {
s, _ := strconv.ParseFloat(value, 64)
return s
}
/*
ConvertStringToFloatScale2Comma o antigo nome era: ConvertStringToFloatScale2Virgula
*/
func ConvertStringToFloatScale2Comma(value string) float64 {
value = strings.Replace(value, "%", "", 1)
value = strings.Replace(value, ".", "", 1)
value = strings.Replace(value, "R", "", 1)
value = strings.Replace(value, "$", "", 1)
value = strings.Replace(value, " ", "", 1)
value = strings.Replace(value, ",", ".", 1)
s, _ := strconv.ParseFloat(value, 64)
return s
}
/*
ConvertStringToFloatScale2Format o antigo nome era: ConvertStringToFloatScale2
*/
func ConvertStringToFloatScale2FormatNumber(value string) float64 {
value = strings.Replace(value, ".", "", 1)
value = strings.Replace(value, ",", "", 1)
if len(value) == 1 {
value = fmt.Sprint("0", value)
}
value = fmt.Sprint(value[:len(value)-2], ".", value[len(value)-2:])
s, _ := strconv.ParseFloat(value, 64)
return s
}
/*
ConvertStringToFloatScale2
*/
func ConvertStringToFloatScale2(value string) float64 {
if value == "" {
return 0
}
s, _ := strconv.ParseFloat(value, 64)
return s
}
/*
ConvertFloatToBrMoneyString
*/
func ConvertFloatToBrMoneyString(value float64) string{
str := fmt.Sprintf("%.2f", value)
split := strings.Split(str, ".")
number := split[0]
decimal := "00"
if len(split) > 0 {
decimal = split[1]
}
var values []string
if len(number) < 3 {
values = append(values, number)
} else {
for i := len(number); i > 0; i -= 3 {
j := i-3
if j < 0 {
j = 0
}
values = append([]string{number[j:i]}, values...)
}
}
return strings.Join(values[:], ".") + `,` + decimal
}