In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today I'll show you how to convert int, float, and string types to each other in Go. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
Preface
Type conversion is often designed in Go development, so this article focuses on recording the methods of converting plastic, floating-point and string types to each other.
Shaping conversion string fmt.Sprintf
Converts an integer to a string, which can be a binary, octal, decimal, or hexadecimal representation.
Format description% b Integer display% o Integer in Octal% d Integer in Decimal% x Integer in hexadecimal,% X Integer in hexadecimal, alphabetical display method fmt.Sprintf ("% d", int32 (111)) / / 111fmt.Sprintf ("% d", int64 (111)) / / 111fmt.Sprintf ("% d") ) / / 111fmt.Sprintf ("% b", 10) / / 1010strconv.Itoa
Convert an integer of type int to a string representation of decimal, and the underlying call is the next method: FormatInt (int64 (I), 10)
/ / Itoa is equivalent to FormatInt (int64 (I), 10) .func Itoa (I int) string {return FormatInt (int64 (I), 10)} usage
We can convert int32 and int64 to int first, and then use this method to convert them.
Strconv.Itoa (1123) / / 1123strconv.FormatInt
Converts an integer to a string, which can be represented by a string in binary 2 to 36.
Input parameter
I: integer of type int64
Base: indicates the base to be converted. It supports base 2 to 36, and the most frequently used is to convert to base 10.
Func FormatInt (I int64, base int) string {} usage
We can convert int32 and int to int64 first, and then use this method to convert them.
Strconv.FormatInt (123,10) / / 123strconv.FormatInt (123,2) / / 1111011 floating point transfer string fmt.Sprintf
Support float32, float64 to string
Fmt.Sprintf ("% f", 3.12344) / / 3.123440 fmt.Sprintf / Control output decimal places fmt.Sprintf (".2f", 323.12344) / / 323.12strconv.FormatFloat input parameter
Floating point numbers of type f:float64
Fmt: after being converted to a string, the type of the string:
'b'(- ddddp ±DDD): binary index
'e' (- d.dddde ±dd): decimal index
'E' (- d.ddddE ±dd): decimal index
'f'(- ddd.dddd): no exponent
'g': use 'eBay' when the index is large, and'f 'for others
'G': use 'eBay' when the index is large, and'f 'for others.
'x' (- 0xd.ddddp ±DDD): hexadecimal fraction and binary index
'X' (- 0Xd.ddP ±DDD): hexadecimal fraction and binary index
Prec: control longitude
If the format is marked as'e','E','f','x','X', then prec represents the number of digits after the decimal point
If the format is marked as' grubbing, prec represents the total number of digits (integer part + decimal part)
BitSize: represents the original type of f. (although the input parameter f is float64, it may be transferred by 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)} use the method strconv.FormatFloat (3.1415926, 'fags, 5, 64) / / 3.14159strconv.FormatFloat (314159.26,' fags, 5, 64) / / 314159.26000 string transfiguration strconv.Atoi
String to int. The default string is decimal, which is equivalent to the decimal conversion simplified version of the next method ParseInt (s, 10,0).
Func Atoi (s string) (int, error) use method strconv.Atoi ("1234") / / 1234 strconv.Atoi ("1234") / / 1 strconv.ParseInt
String to int32 int64 int, strings can be of different binary types.
/ / s: the string form of a number / / base: the binary of a numeric string, supporting 0 and 2-36. If the field is 0, the binary is inferred based on the prefix of the string, such as "0b"-> 2, "0 or 0o"-> octal, "0x"-> hexadecimal / / bitSize: the bit size of the returned result, 0-> int, 8-> int8, 16-> int16, 32-> int32, 64-> int64. Because you can choose different bitSize, so unified to int64, will not lose precision. BitSize available on demand Turn back to func ParseInt (s string, base int, bitSize int) (I int64, err error) using method / / transfer int32num, err: = strconv.ParseInt ("123,10Magi 32) fmt.Println (int32 (num), err) / / 123 / / transfer int64num, err: = strconv.ParseInt (" 123,10,64) fmt.Println (num, err) / / 123 / transfer intnum, err: = strconv.ParseInt ("123,10,64) fmt.Println (int (num)) Err) / / 123 / / binary to int64num, err: = strconv.ParseInt ("0b1001", 0,64) fmt.Println (int (num), err) / / 9 string to floating point strconv.ParseFloat// s: string / / bitSize: precision / / method to return result always returns float64 Even if bitSize=32 Return float64 will 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)} use method strconv.ParseFloat ("123.213", 64) / / 123.213 strconv.ParseFloat ("123213") 64) / / more than 123213 is all about the conversion between int, float and string types in Go. For more information about the conversion between int, float and string types in the Go language, you can search for previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.