zoukankan      html  css  js  c++  java
  • golang类型转换小总结

    1. int <--> string

    1.1. int --> string

    str := strconv.Itoa(intVal)

    当然,整数转换成字符串还有其他方法,比如

    fmt.Sprintf
    strconv.FormatInt

    1.2. string --> int

    intVal,err := strconv.Atoi(str)

    2. string --> int64

    2.1. string --> int64

    int64Val,err := strconv.ParseInt(str, 10, 64)

    2.2. int64 --> string

    str := strconv.FormatInt(int64Val, 10)

    其中FormatInt第二个参数表示进制,10表示十进制

    需要注意转换规定

    FormatInt returns the string representation of i in the given base, for 2 <= base <= 36.
    The result uses the lower-case letters 'a' to 'z' for digit values >= 10
     

    3. float --> string

    3.1. float --> string

    v := 3.1415926535
    s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32
    s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64
     
    第二个参数可选'f'/'e'/'E'等,含义如下:
     'b' (-ddddp±ddd,二进制指数)
     'e' (-d.dddde±dd,十进制指数)
     'E' (-d.ddddE±dd,十进制指数)
     'f' (-ddd.dddd,没有指数)
     'g' ('e':大指数,'f':其它情况)
     'G' ('E':大指数,'f':其它情况)
     

    3.2. string --> float

    s := "3.1415926535"
    v1, err := strconv.ParseFloat(v, 32)
    v2, err := strconv.ParseFloat(v, 64)

    4.float --> int

    var a int64
    a = 1
    var b float64
    b = 2.000

    4.1. float --> int

    d := int64(b)

    4.2. int --> float

    c := float64(a)
     
  • 相关阅读:
    希尔排序
    Java内存区域与内存溢出异常
    插入排序
    选择排序
    冒泡排序
    专利申请笔记
    Python基础指北
    mini web
    linux i/o multiplexing
    Python decorator module
  • 原文地址:https://www.cnblogs.com/justdoyou/p/10042176.html
Copyright © 2011-2022 走看看