zoukankan      html  css  js  c++  java
  • golang数据类型二

     

     

     

     

     

     

     

    字符类型

     

     

     

     

     

     

     

     3.14基本数据类型的相互转换

     

     

     

    3.15基本数据类型和string的转换

     

     

    FormatInt

    // FormatUint 将 int 型整数 i 转换为字符串形式
    // base:进位制(2 进制到 36 进制)
    // 大于 10 进制的数,返回值使用小写字母 'a' 到 'z'
    func FormatInt(i int64, base int) string

    func main(){
    	var num1 int = 99
    	//var num2 float64 = 23.456
    	//var b bool = true
    	var str string
    
    	str = strconv.FormatInt(int64(num1),2)
    	fmt.Printf("str type %T  str=%q
    ",str,str)
    
    }
    

      返回:

    str type string  str="1100011"
    

      

    FormatFloat 

    / FormatFloat 将浮点数 f 转换为字符串值
    // f:要转换的浮点数
    // fmt:格式标记(b、e、E、f、g、G)
    // prec:精度(数字部分的长度,不包括指数部分)
    // bitSize:指定浮点类型(32:float32、64:float64)
    //
    // 格式标记:
    // 'b' (-ddddp±ddd,二进制指数)
    // 'e' (-d.dddde±dd,十进制指数)
    // 'E' (-d.ddddE±dd,十进制指数)
    // 'f' (-ddd.dddd,没有指数)
    // 'g' ('e':大指数,'f':其它情况)
    // 'G' ('E':大指数,'f':其它情况)
    //
    // 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数
    // 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分)
    func FormatFloat(f float64, fmt byte, prec, bitSize int) string

    func main() {
    	f := 100.12345678901234567890123456789
    	fmt.Println(strconv.FormatFloat(f, 'b', 5, 32))
    	// 13123382p-17
    	fmt.Println(strconv.FormatFloat(f, 'e', 5, 32))
    	// 1.00123e+02
    	fmt.Println(strconv.FormatFloat(f, 'E', 5, 32))
    	// 1.00123E+02
    	fmt.Println(strconv.FormatFloat(f, 'f', 5, 32))
    	// 100.12346
    	fmt.Println(strconv.FormatFloat(f, 'g', 5, 32))
    	// 100.12
    	fmt.Println(strconv.FormatFloat(f, 'G', 5, 32))
    	// 100.12
    	fmt.Println(strconv.FormatFloat(f, 'b', 30, 32))
    	// 13123382p-17
    	fmt.Println(strconv.FormatFloat(f, 'e', 30, 32))
    	// 1.001234588623046875000000000000e+02
    	fmt.Println(strconv.FormatFloat(f, 'E', 30, 32))
    	// 1.001234588623046875000000000000E+02
    	fmt.Println(strconv.FormatFloat(f, 'f', 30, 32))
    	// 100.123458862304687500000000000000
    	fmt.Println(strconv.FormatFloat(f, 'g', 30, 32))
    	// 100.1234588623046875
    	fmt.Println(strconv.FormatFloat(f, 'G', 30, 32))
    	// 100.1234588623046875
    }
    

      

    FormatBool 

    // FormatBool 将布尔值转换为字符串 "true" 或 "false"
    func FormatBool(b bool) string

    func main() {
    	fmt.Println(strconv.FormatBool(0 < 1)) // true
    	fmt.Println(strconv.FormatBool(0 > 1)) // false
    }
    

      

    func FormatInt

    func FormatInt(i int64, base int) string

    返回i的base进制的字符串表示。base 必须在2到36之间,结果中会使用小写字母'a'到'z'表示大于10的数字。

    // Itoa 相当于 FormatInt(i, 10)
    func Itoa(i int) string

    func main() {
    	fmt.Println(strconv.Itoa(-2048)) // -2048
    	fmt.Println(strconv.Itoa(2048))  // 2048
    }
    

      

    string类型转基本数据类型

  • 相关阅读:
    如何将 Web 框架迁移到 Serverless
    Serverless + CVM 实战
    Serverless Registry 设计解读与实战
    使用 ServerLess 实现云原生
    腾讯云 Serverless 技术在「老司机汽车 app」的落地实践
    LeetCode 树 103. 二叉树的锯齿形层次遍历(双端队列 DFS的空间复杂度)
    LeetCode 树 116. 填充每个节点的下一个右侧节点指针(层序遍历 分层)
    LeetCode 树 230. 二叉搜索树中第K小的元素(二叉搜索树 中序遍历 剪枝)
    LeetCode 树 236. 二叉树的最近公共祖先(递归 深度优先搜索)
    LeetCode 树 102. 二叉树的层序遍历(广度优先搜索 深度优先搜索 队列)
  • 原文地址:https://www.cnblogs.com/sunlong88/p/10988972.html
Copyright © 2011-2022 走看看