go ParseFloat()方法
ParseFloat()语法
func ParseFloat(s string, bitSize int) (f float64, err error)
go源码中对ParseFloat()方法的介绍:
ParseFloat converts the string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.
ParseFloat accepts decimal and hexadecimal floating-point number syntax. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding. (Parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal
即,golang的标准库中的strconv模块中的ParseFloat()方法,可以将s字符串转换为浮点数类型的值,并返回两个值。
参数
参数 | 描述 |
---|---|
s | golang string字符串类型 |
bitSize | 指定期望的接收类型,32表示float32,64表示float64 |
返回值
strconv.ParseFloat()方法,可返回两个值,f为转换得到浮点数类型值,err一般为*NumErr类型,如果是语法有误,则err.Error=ErrSyntax;如果转换得到的值超出表示范围,则返回值f为±Inf,err.Error= ErrRange。
strings.ParseFloat()实例代码
func main() {
f, err := strconv.ParseFloat("0.36", 64)
if err == nil {
fmt.Println(f)
} else {
fmt.Println(err)
}
}
运行go文件,得到输出:
0.36