strconv
Index
- func Atoi(s string) (int, error)
- func FormatBool(b bool) string
- func FormatFloat(f float64, fmt byte, prec, bitSize int) string
- func FormatInt(i int64, base int) string
- func FormatUint(i uint64, base int) string
- func Itoa(i int) string
- func ParseBool(str string) (bool, error)
- func ParseFloat(s string, bitSize int) (float64, error)
- func ParseInt(s string, base int, bitSize int) (i int64, err error)
- func ParseUint(s string, base int, bitSize int) (uint64, error)
Functions
func Atoi
1func Atoi(s string) (int, error)
Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
1v := "10"
2if s, err := strconv.Atoi(v); err == nil {
3 fmt.Printf("%T, %v", s, s)
4}
Output
int, 10
func FormatBool
1func FormatBool(b bool) string
FormatBool returns "true" or "false" according to the value of b.
1v := true
2s := strconv.FormatBool(v)
3fmt.Printf("%T, %v\n", s, s)
Output
string, true
func FormatFloat
1func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64).
The format fmt is one of ‘b’ (-ddddp±ddd, a binary exponent), ’e’ (-d.dddde±dd, a decimal exponent), ‘E’ (-d.ddddE±dd, a decimal exponent), ‘f’ (-ddd.dddd, no exponent), ‘g’ (’e’ for large exponents, ‘f’ otherwise), ‘G’ (‘E’ for large exponents, ‘f’ otherwise), ‘x’ (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or ‘X’ (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
The precision prec controls the number of digits (excluding the exponent) printed by the ’e’, ‘E’, ‘f’, ‘g’, ‘G’, ‘x’, and ‘X’ formats. For ’e’, ‘E’, ‘f’, ‘x’, and ‘X’, it is the number of digits after the decimal point. For ‘g’ and ‘G’ it is the maximum number of significant digits (trailing zeros are removed). The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly.
1v := 3.1415926535
2
3s32 := strconv.FormatFloat(v, 'E', -1, 32)
4fmt.Printf("%T, %v\n", s32, s32)
5
6s64 := strconv.FormatFloat(v, 'E', -1, 64)
7fmt.Printf("%T, %v\n", s64, s64)
Output
string, 3.1415927E+00 string, 3.1415926535E+00
func FormatInt
1func FormatInt(i int64, base int) string
FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.
1v := int64(-42)
2
3s10 := strconv.FormatInt(v, 10)
4fmt.Printf("%T, %v\n", s10, s10)
5
6s16 := strconv.FormatInt(v, 16)
7fmt.Printf("%T, %v\n", s16, s16)
Output
string, -42 string, -2a
func FormatUint
1func FormatUint(i uint64, base int) string
FormatUint returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.
1v := uint64(42)
2
3s10 := strconv.FormatUint(v, 10)
4fmt.Printf("%T, %v\n", s10, s10)
5
6s16 := strconv.FormatUint(v, 16)
7fmt.Printf("%T, %v\n", s16, s16)
Output
string, 42 string, 2a
func Itoa
1func Itoa(i int) string
Itoa is equivalent to FormatInt(int64(i), 10).
1i := 10
2s := strconv.Itoa(i)
3fmt.Printf("%T, %v\n", s, s)
Output
string, 10
func ParseBool
1func ParseBool(str string) (bool, error)
ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.
1v := "true"
2if s, err := strconv.ParseBool(v); err == nil {
3 fmt.Printf("%T, %v\n", s, s)
4}
Output
bool, true
func ParseFloat
1func ParseFloat(s string, bitSize int) (float64, error)
ParseFloat converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.
ParseFloat accepts decimal and hexadecimal floating-point numbers as defined by the Go syntax for floating-point literals. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding. (Parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa.)
The errors that ParseFloat returns have concrete type *NumError and include err.Num = s.
If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.
ParseFloat recognizes the string “NaN”, and the (possibly signed) strings “Inf” and “Infinity” as their respective special floating point values. It ignores case when matching.
1v := "3.1415926535"
2if s, err := strconv.ParseFloat(v, 32); err == nil {
3 fmt.Printf("%T, %v\n", s, s)
4}
5if s, err := strconv.ParseFloat(v, 64); err == nil {
6 fmt.Printf("%T, %v\n", s, s)
7}
8if s, err := strconv.ParseFloat("NaN", 32); err == nil {
9 fmt.Printf("%T, %v\n", s, s)
10}
11
12if s, err := strconv.ParseFloat("nan", 32); err == nil {
13 fmt.Printf("%T, %v\n", s, s)
14}
15if s, err := strconv.ParseFloat("inf", 32); err == nil {
16 fmt.Printf("%T, %v\n", s, s)
17}
18if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
19 fmt.Printf("%T, %v\n", s, s)
20}
21if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
22 fmt.Printf("%T, %v\n", s, s)
23}
24if s, err := strconv.ParseFloat("-0", 32); err == nil {
25 fmt.Printf("%T, %v\n", s, s)
26}
27if s, err := strconv.ParseFloat("+0", 32); err == nil {
28 fmt.Printf("%T, %v\n", s, s)
29}
Output
float64, 3.1415927410125732 float64, 3.1415926535 float64, NaN float64, NaN float64, +Inf float64, +Inf float64, -Inf float64, -0 float64, 0
func ParseInt
1func ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.
The string may begin with a leading sign: “+” or “-”.
If the base argument is 0, the true base is implied by the string’s prefix following the sign (if present): 2 for “0b”, 8 for “0” or “0o”, 16 for “0x”, and 10 otherwise. Also, for argument base 0 only, underscore characters are permitted as defined by the Go syntax for integer literals.
The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64. If bitSize is below 0 or above 64, an error is returned.
The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.
1v32 := "-354634382"
2if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
3 fmt.Printf("%T, %v\n", s, s)
4}
5if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
6 fmt.Printf("%T, %v\n", s, s)
7}
8
9v64 := "-3546343826724305832"
10if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
11 fmt.Printf("%T, %v\n", s, s)
12}
13if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
14 fmt.Printf("%T, %v\n", s, s)
15}
Output
int64, -354634382 int64, -3546343826724305832
func ParseUint
1func ParseUint(s string, base int, bitSize int) (uint64, error)
ParseUint is like ParseInt but for unsigned numbers.
A sign prefix is not permitted.
1v := "42"
2if s, err := strconv.ParseUint(v, 10, 32); err == nil {
3 fmt.Printf("%T, %v\n", s, s)
4}
5if s, err := strconv.ParseUint(v, 10, 64); err == nil {
6 fmt.Printf("%T, %v\n", s, s)
7}
Output
uint64, 42 uint64, 42