Commit b3f624151c72f956ec31e275ec20e70fe4199250
1 parent
ff845040c1
Exists in
master
StringToValidInt64()
Showing
1 changed file
with
8 additions
and
0 deletions
Show diff stats
int_util.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "strconv" | 5 | "strconv" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | // ClampInt64 ... | 8 | // ClampInt64 ... |
9 | func ClampInt64(v, min, max int64) int64 { | 9 | func ClampInt64(v, min, max int64) int64 { |
10 | if v < min { | 10 | if v < min { |
11 | return min | 11 | return min |
12 | } else if v > max { | 12 | } else if v > max { |
13 | return max | 13 | return max |
14 | } | 14 | } |
15 | 15 | ||
16 | return v | 16 | return v |
17 | } | 17 | } |
18 | 18 | ||
19 | // InRangeInt64 ... | 19 | // InRangeInt64 ... |
20 | func InRangeInt64(v, min, max int64) bool { | 20 | func InRangeInt64(v, min, max int64) bool { |
21 | return (v >= min && v <= max) | 21 | return (v >= min && v <= max) |
22 | } | 22 | } |
23 | 23 | ||
24 | // StringToInt64 ... | 24 | // StringToInt64 ... |
25 | func StringToInt64(s string) int64 { | 25 | func StringToInt64(s string) int64 { |
26 | i, _ := strconv.ParseInt(s, 10, 64) | 26 | i, _ := strconv.ParseInt(s, 10, 64) |
27 | return i | 27 | return i |
28 | } | 28 | } |
29 | 29 | ||
30 | // StringToFloat64 ... | 30 | // StringToFloat64 ... |
31 | func StringToFloat64(s string) float64 { | 31 | func StringToFloat64(s string) float64 { |
32 | f, _ := strconv.ParseFloat(s, 64) | 32 | f, _ := strconv.ParseFloat(s, 64) |
33 | return f | 33 | return f |
34 | } | 34 | } |
35 | 35 | ||
36 | // Int64ToString ... | 36 | // Int64ToString ... |
37 | func Int64ToString(i int64) string { | 37 | func Int64ToString(i int64) string { |
38 | return fmt.Sprintf("%d", i) | 38 | return fmt.Sprintf("%d", i) |
39 | } | 39 | } |
40 | 40 | ||
41 | // BoolToInt64 ... | 41 | // BoolToInt64 ... |
42 | func BoolToInt64(b bool) int64 { | 42 | func BoolToInt64(b bool) int64 { |
43 | if b { | 43 | if b { |
44 | return 1 | 44 | return 1 |
45 | } | 45 | } |
46 | return 0 | 46 | return 0 |
47 | } | 47 | } |
48 | 48 | ||
49 | // Int64ToBool ... | 49 | // Int64ToBool ... |
50 | func Int64ToBool(i int64) bool { | 50 | func Int64ToBool(i int64) bool { |
51 | return i != 0 | 51 | return i != 0 |
52 | } | 52 | } |
53 | |||
54 | func StringToValidInt64(s string) (int64, bool) { | ||
55 | i, err := strconv.ParseInt(s, 10, 64) | ||
56 | if err != nil { | ||
57 | return i, false | ||
58 | } | ||
59 | return i, true | ||
60 | } | ||
53 | 61 |