Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report