-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52_number_parsing.go
More file actions
35 lines (28 loc) · 854 Bytes
/
52_number_parsing.go
File metadata and controls
35 lines (28 loc) · 854 Bytes
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
package gobyexample
import (
"fmt"
"strconv"
)
// NumberParsingDemo - demonstrates number parsing
func NumberParsingDemo() {
// With `ParseFloat`, this 64 indicates how many
// bits of precision to parse
f, _ := strconv.ParseFloat("1.234", 64)
fmt.Println(f)
// With `ParseInt`, the 0 means infer the base from
// the string. 64 requires that the result fit in 64 bits.
i, _ := strconv.ParseInt("123", 0, 64)
fmt.Println(i)
// `ParseInt` will recognize hexadecimal numbers.
d, _ := strconv.ParseInt("0x1c8", 0, 64)
fmt.Println(d)
// A `ParseUint` is also available.
u, _ := strconv.ParseUint("789", 0, 64)
fmt.Println(u)
// `Atoi` is a convenience function for basic base-10 int parsing.
k, _ := strconv.Atoi("135")
fmt.Println(k)
// Parse functions return an error on bad input.
_, e := strconv.Atoi("wot")
fmt.Println(e)
}