Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the conversion among string, int and float in go

Shulou Source: shulou.com Published: 2022-06-01 04:43:45 09月23日 Update

Editor to share with you how to achieve the conversion of string, int and float in go. I hope you will get something after reading this article. Let's discuss it together.

String to other string-- > int: convert the string 13 to a numeric value of type int 13str: = "13" distInt,err: = strconv.Atoi (str) string-- > int64: convert the string 13 to int64 (decimal) / / Parameter 1: with converted string, / / Parameter 2: based on the decimal system, the value can be 0per8, 16, 32, 64, 3: which int type to convert: 0, 8, 16, 32, 64 Corresponding to int,int8,int16,int32,int64distInt64, err: = strconv.ParseInt (str, 10,64) string-> float64, float32// ParseFloat converts a string to a floating point number / / str: string to convert / / bitSize: specify floating point type (32:float32, 64:float64) / / if str is a legal format and is close to a floating point value / / returns the floating point rounding value (according to IEEE754's rounding criteria) / / returns "syntax error" if str is not a legal format / / if the conversion result is outside the bitSize range Return "out of range" / / to float64distFloat,err: = strconv.ParseFloat (str,64) / / to float32distFloat,err: = strconv.ParseFloat (str,32) int, int64 to another int-> stringi: = 11str1: = strconv.Itoa (I) / / or str2: = strconv.FormatInt (int64 (I), 10) int64-> stringdistStr: = strconv.FormatInt (num) 10) float to other float-- > string// FormatFloat converts floating point number f to string value / / f: floating point number to convert / / fmt: format tag (b, e, E, f, g, G) / / prec: precision (length of numeric part) Does not include index part) / / bitSize: specify floating point type (32:float32, 64:float64) / / format tag: / /'b' (- ddddp ±DDD, binary index) / /'e' (- d.dddde ±dd, decimal index) / / E' (- d.ddddE ±dd, decimal index) / /'f'(- ddd.dddd, no index) / /'g' ('evalues: large index) / / prec represents the number of digits after the decimal point / / if the format is marked as' estrangement,'or 'fags, then the number of digits after the decimal point / / if the format is marked as' gallop Then prec denotes the total number of digits (integer part + decimal part) str1 = strconv.FormatFloat (11.34 string strconv.ParseFloat (str1,32) fmt.Println (h) / 11.340015258789h). _ = strconv.ParseFloat (str2,64) fmt.Println (h) / / 10.55float64-- > int64 (there will be a loss of accuracy) var x float64 = 6.9y: = int64 (x) finished reading this article I believe you have a certain understanding of "how to convert string, int and float in go". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Tags: Exponent character string format mark point type part decimal parameter number floating point decimal legal rounded digits decimal case article precision Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Apple Shulou Technology Microsoft