-
Notifications
You must be signed in to change notification settings - Fork 18
/
Float.go
59 lines (51 loc) · 1.3 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
package F
// Floating-point/Real number support package
import (
"strconv"
"time"
)
// If simplified ternary operator (bool ? val : 0), returns second argument, if the condition (first arg) is true, returns 0 if not
//
// F.If(true,3.12) // 3.12
// F.If(false,3) // 0
func If(b bool, yes float64) float64 {
if b {
return yes
}
return 0
}
// IfElse ternary operator (bool ? val1 : val2), returns second argument if the condition (first arg) is true, third argument if not
//
// F.IfElse(true,3.12,3.45)) // 3.12
func IfElse(b bool, yes, no float64) float64 {
if b {
return yes
}
return no
}
// ToS convert float64 to string
//
// F.ToS(3.1284)) // `3.1284`
func ToS(num float64) string {
return strconv.FormatFloat(num, 'f', -1, 64)
}
// ToStr convert float64 to string with 2 digits behind the decimal point
//
// F.ToStr(3.1284)) // `3.13`
func ToStr(num float64) string {
return strconv.FormatFloat(num, 'f', 2, 64)
}
// ToIsoDateStr convert to ISO-8601 string
//
// F.ToIsoDateStr(0) // `1970-01-01T00:00:00`
func ToIsoDateStr(num float64) string {
n := int64(num)
t := time.Unix(n, 0)
return t.UTC().Format(`2006-01-02T15:04:05Z`)
}
// ToDateStr convert float64 unix to `YYYY-MM-DD`
func ToDateStr(num float64) string {
n := int64(num)
t := time.Unix(n, 0)
return t.UTC().Format(`2006-01-02`)
}