Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert a string to a floating-point number in go

Shulou Source: shulou.com Published: 2022-05-31 22:06:41 10月27日 Update

This article introduces the knowledge of "how to convert a string into a floating point number in go". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Go language method for converting strings to floating-point numbers: using the ParseFloat () function

The ParseFloat () function is used to convert a string representing a floating point number to a float type, signed as follows.

Func ParseFloat (s string, bitSize int) (f float64, err error)

Parameter description:

If s conforms to the grammatical rules, the function returns a floating-point number closest to the value represented by s (rounded using the IEEE754 specification).

BitSize specifies the type of return value. 32 means float32,64 means float64.

The return value err is of type * NumErr. If the syntax is incorrect err.Error=ErrSyntax, if the return value is out of the range of representation, the return value f is ±Inf,err.Error= ErrRange.

The sample code is as follows:

Package mainimport ("fmt"strconv") func main () {str: = "3.1415926" num, err: = strconv.ParseFloat (str, 64) if err! = nil {fmt.Println (err)} else {fmt.Println (num)}}

The running results are as follows:

3.1415926

Description:

The function all has two return values, the first of which is the converted value, and the second is the error message that the conversion failed.

That's all for "how to convert a string to a floating point number in the go language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Points functions characters strings languages types lieutenant generals content more knowledge grammar practicality learning and then two code information parameters dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Shulou Information Xiaomi macOS