今天在写golang时把字符串"100.00"整数型转成了0了!!!这里记录下......
下面直接上代码!!!


错误代码

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "100.00"
    strInt,err := strconv.Atoi(str)
    fmt.Println(str)
    fmt.Println(strInt,err)
}

输出

100.00
0 strconv.Atoi: parsing "100.00": invalid syntax

原因

因为100.00不是一个整数



下面是源码中的注释

ParseInt interprets a string s in the given base (2 to 36) and
returns the corresponding value i.  If base == 0, the base is
implied by the string's prefix: base 16 for "0x", base 8 for
"0", and base 10 otherwise.

The bitSize argument specifies the integer type
that the result must fit into.  Bit sizes 0, 8, 16, 32, and 64
correspond to int, int8, int16, int32, and int64.

The errors that ParseInt returns have concrete type *NumError
and include err.Num = s.  If s is empty or contains invalid
digits, err.Err = ErrSyntax and the returned value is 0;
if the value corresponding to s cannot be represented by a
signed integer of the given size, err.Err = ErrRange and the
returned value is the maximum magnitude integer of the
appropriate bitSize and sign.

2019-12-28T10:51:00.png


本文由 吾爱 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论