-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberparse.go
48 lines (39 loc) · 921 Bytes
/
numberparse.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
package main
//
// The built-in package strconv provides
// number parsing.
//
import (
"fmt"
"strconv"
)
func main() {
//
// With ParseFloat, this 64 tells how many bits
// of precision to parse.
//
f, _ := strconv.ParseFloat("1.234", 64)
fmt.Println(f)
//
// FOr ParseInt, the 0 means infer the base from
// the string. 64 requires that the result fit
// in the64 bites.
//
i, _ := strconv.ParseInt("123", 0, 64)
fmt.Println(i)
// ParseInt will recognize hex-formatted numbers.
d, _ := strconv.ParseInt("0x1c8", 0, 64)
fmt.Println(d)
// A ParseUint iss also available
u, _ := strconv.ParseUint("789", 0, 64)
fmt.Println(u)
//
// Atoi is a convenience funciton for basic
// base-10 int parsing.
//
k, _ := strconv.Atoi("135")
fmt.Println(k)
// Parse functions return an error on bad input.
_, e := strconv.Atoi("wat")
fmt.Println(e)
}