You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

Type conversions are often designed in Go development, and this article focuses on ways to convert integer, floating point, and string types to each other.

Transform string

fmt.Sprintf

Converts an integer to a string, which can be a base 2, 8, 10, or 16 representation.

format describe
%b Integers are displayed in binary mode
%o Integer types are displayed in octal mode
%d Integers are displayed in decimal format
%x An integer is displayed in hexadecimal format
%X Integers are displayed in hexadecimal and uppercase letters

Method of use

fmt.Sprintf("%d".int32(111))   / / 111
fmt.Sprintf("%d".int64(111))   / / 111
fmt.Sprintf("%d".111)					/ / 111
fmt.Sprintf("%b".10)						/ / 1010

Copy the code

strconv.Itoa

FormatInt(int64(I), 10) ¶ Convert an int to a base 10 string representation. The next method is called: FormatInt(int64(I), 10)

// Itoa is equivalent to FormatInt(int64(i), 10).
func Itoa(i int) string {
	return FormatInt(int64(i), 10)}Copy the code

Method of use

Int int32, int64; int int32, int64

strconv.Itoa(1123)  / / 1123
Copy the code

strconv.FormatInt

Converts an integer to a string, which can be expressed in bases 2 to 36.

The ginseng

  • I: integer of type int64
  • Base: indicates the base to be converted to, which can be from 2 to 36. The most commonly used base is converted to 10
func FormatInt(i int64, base int) string {}
Copy the code

Method of use

We can convert int32 and int to int64 and then use this method

strconv.FormatInt(123.10)  / / 123
strconv.FormatInt(123.2)   / / 1111011
Copy the code

Floating point to string

fmt.Sprintf

Supports float32 and float64 to String

fmt.Sprintf("%f".3.12344)  / / 3.123440

// Control the output decimal number
fmt.Sprintf("%.2f".323.12344)  / / 323.12
Copy the code

strconv.FormatFloat

The ginseng

  • F: Floating point number of type float64

  • FMT: The type of the string after being converted to a string:

    • ‘b’ (-DDDDP ± DDD) : binary exponent

    • ‘e’ (-d.ddde ±dd) : decimal index

    • ‘E’ (-d.ddde ±dd) : decimal index

    • ‘f’ (-ddd.dddd) : no index

    • ‘g’ : use ‘e’ if the index is large, ‘f’ otherwise

    • ‘G’ : use ‘E’ if the index is large, ‘f’ otherwise

    • ‘x’ (-0xd.dddDP ± DDD) : hexadecimal fraction and binary exponent

    • ‘X’ (-0xd.dddDP ± DDD) : hexadecimal fraction and binary exponent

  • Prec: Controls longitude

    • If the format is marked ‘e’, ‘e’, ‘f’, ‘x’, ‘x’, then prec represents the number of digits after the decimal point

    • If the format is marked ‘g’, ‘g’, then prec represents the total number of digits (integer part + decimal part)

  • BitSize: indicates the initial type of f. (although the input parameter f is float64, it may be converted from float32)

func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
	return string(genericFtoa(make([]byte.0, max(prec+4.24)), f, fmt, prec, bitSize))
}
Copy the code

Method of use

strconv.FormatFloat(3.1415926.'f'.5.64)  / / 3.14159

strconv.FormatFloat(3.1415926 e5.'f'.5.64) / / 314159.26000
Copy the code

String shaping

strconv.Atoi

String to int, the default string is in base 10, which is the equivalent of ParseInt(s, 10, 0)

func Atoi(s string) (int, error)
Copy the code

Method of use

strconv.Atoi("1234")  // 1234 <nil>

strconv.Atoi("001") // 1 <nil>

Copy the code

strconv.ParseInt

String to int32 int64 int, string can be of different base types.

// s: a string of digits

// base: indicates the base of a numeric string. The value can be 0 or 2-36. If the field is 0, the base is inferred based on the prefix of the string, such as "0b"-> base 2, "0 or 0O "-> base 8, and "0x" -> base 16

// bitSize: The returned bit size, 0 -> int, 8 -> int8, 16 -> int16, 32 -> int32, 64 -> int64. Since you can choose different bitsizes, you can convert them to INT64 without losing accuracy. It can be rotated back according to the required bitSize
func ParseInt(s string, base int, bitSize int) (i int64, err error)

Copy the code

Method of use

// 转int32
num, err := strconv.ParseInt("123".10.32)
fmt.Println(int32(num), err)  // 123 <nil>

// 转int64
num, err := strconv.ParseInt("123".10.64)
fmt.Println(num, err) // 123 <nil>

// 转int
num, err := strconv.ParseInt("123".10.64)    
fmt.Println(int(num), err) // 123 <nil>

// Convert binary to int64
num, err := strconv.ParseInt("0b1001".0.64)
fmt.Println(int(num), err)  // 9 <nil>
Copy the code

The string is converted to floating point

strconv.ParseFloat

// s: string
// bitSize: The precision of the result to return
The // method always returns float64, even if bitSize=32, returning float64 does not lose precision

func ParseFloat(s string, bitSize int) (float64, error) {
	if bitSize == 32 {
		f, err := atof32(s)
		return float64(f), err
	}
	return atof64(s)
}
Copy the code

Method of use

strconv.ParseFloat("123.213".64)   / / 123.213 < nil >


strconv.ParseFloat("123.213 e3".64)  // 123213 <nil>

Copy the code

conclusion

This article introduces the interconversion method of integer, floating point, string, can remember FMT.Sprintf method, as well as the Strconv package, when using the IDE method tips can be done!

More and more

Personal blog: lifelmy.github. IO /

Wechat official account: Long Coding road