-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_text.go
62 lines (55 loc) · 1.25 KB
/
number_text.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
package omniconv
import (
"fmt"
"strconv"
)
// IntToStringConverter converts integer into string
func IntToStringConverter[T Int](from T) (to string) {
return fmt.Sprintf("%d", from)
}
// FloatToStringConverter converts float into string
func FloatToStringConverter[T Float](from T) (to string) {
return fmt.Sprintf("%f", from)
}
// StringToIntConverter parses int from string
//
// On error returns default value (0)
func StringToIntConverter[T Int](from string) (to T) {
n, err := strconv.Atoi(from)
if err != nil {
return 0
}
return T(n)
}
// MustStringToIntConverter parses int from string
//
// On error panics
func MustStringToIntConverter[T Int](from string) (to T) {
n, err := strconv.Atoi(from)
if err != nil {
panic(err)
}
return T(n)
}
// StringToFloatConverter parses float from string
//
// On error returns default value (0)
func StringToFloatConverter[T Float](from string) (to T) {
const bitSize = 64
n, err := strconv.ParseFloat(from, bitSize)
if err != nil {
return 0
}
return T(n)
}
// MustStringToFloatConverter parses float from string
//
// On error panics
func MustStringToFloatConverter[T Float](from string) (to T) {
const bitSize = 64
n, err := strconv.ParseFloat(from, bitSize)
if err != nil {
panic(err)
}
return T(n)
}