strconv实现了go中基本数据类型与string之间的转换。

How to use in go

go doc:https://godoc.org/strconv

import "strconv"

int ↔ string

func Atoi(s string) (int, error)

  将string类型的s转换为十进制int类型,同时会返回error。

func Itoa(i int) string

  将十进制i转换为string类型。

// int ↔ string
stringValue := "150"
intValue, err := strconv.Atoi(stringValue)
if err != nil {
fmt.Printf("Failed to conv string to int: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, intValue, intValue) //string 150 conv to int 150
}
stringValue = strconv.Itoa(intValue)
fmt.Printf("%T %d conv to %T %s\n", intValue, intValue, stringValue, stringValue) //int 150 conv to string 150

int64 ↔ string

func ParseInt(s string, base int, bitSize int) (i int64, err error)

  将string类型的s转换为base进制下bitSize比特的i,base的取值范围是{0}∪[2,36],bitSize的取值范围是[0,64]。当base=0时,字符串的前缀表示基数,比如“0x”表示十六进制,“0”表示八进制,其他表示十进制;当base=1 || base<0 || base>36,返回error。bitSize指定了整型的位数,bitSize=0,8,16,32和64分别对应了int,int8,int16,int32和int64。

func FormatInt(i int64, base int) string

  将i转换为base进制下的string类型,base的的取值范围是[2,36]。

// int64 ↔ string
int64Value, err := strconv.ParseInt(stringValue, 10, 64)
if err != nil {
fmt.Printf("Failed to conv string to int64: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, int64Value, int64Value) //string 150 conv to int64 150
}
stringValue = strconv.FormatInt(int64Value, 10)
fmt.Printf("%T %d conv to %T %s\n", int64Value, int64Value, stringValue, stringValue) //int64 150 conv to string 150

uint64 ↔ string

func ParseUint(s string, base int, bitSize int) (uint64, error)

  ParseUint的用法与ParseInt相同,返回的结果是uint64类型。

func FormatUint(i uint64, base int) string

  将uint64类型按照给定base转换为string类型,base的取值范围是[2,36]。

// uint64 ↔ string
uint64Value, err := strconv.ParseUint(stringValue, 10, 64)
if err != nil {
fmt.Printf("Failed to conv string to uint64: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %d\n", stringValue, stringValue, uint64Value, uint64Value) //string 150 conv to uint64 150
}
stringValue = strconv.FormatUint(uint64Value, 10)
fmt.Printf("%T %d conv to %T %s\n", uint64Value, uint64Value, stringValue, stringValue) //uint64 150 conv to string 150

float64 ↔ string

func ParseFloat(s string, bitSize int) (float64, error)

  ParseFloat将字符串s转换为浮点数,精度由bitSize指定,bitSize=32转换为float32,64转换为float64。 当bitSize = 32时,结果仍然是float64类型,但不改变值就可以转换为float32。

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

  FormatFloat根据格式fmt和精度prec将浮点数f转换为字符串。

  格式fmt取值有几种,'b'表示二进制指数( - ddddp±ddd),'e'表示十进制指数( - d.dddde±dd),'E'表示十进制指数(- ddddd±dd),'f'表示无指数(- ddd.dddd),'g'表示对于大指数使用格式'e',否则使用'f','G'表示对于大指数使用格式'E',否则使用'f'。

  精度是由格式'e','E','f','g'和'G'控制。 对于'e','E'和'f',是小数点后的位数。 对于'g'和'G',是最大有效位数(删除末尾的零)。当prec=-1时,表示使用所需的最小位数。

// float64 ↔ string
float64Value,err:=strconv.ParseFloat("3.1415926535",64)
if err !=nil{
fmt.Printf("Failed to conv string to float64: %s",err)
}else{
fmt.Printf("%T %s conv to %T %v\n","3.1415926535","3.1415926535",float64Value,float64Value) //string 3.1415926535 conv to float64 3.1415926535
}
stringValue=strconv.FormatFloat(float64Value,'E',-1,64)
fmt.Printf("%T %v conv to %T %s\n",float64Value,float64Value,stringValue,stringValue) //float64 3.1415926535 conv to string 3.1415926535E+00

bool ↔ string

func ParseBool(str string) (bool, error)

  返回string类型的str表示的bool值,str∈{1,t,T,TRUE,true,True,0,f,F,FALSE,false,False},如果str是其他值,函数返回error。

func FormatBool(b bool) string

  根据b的值返回“true”或“false”。

// bool ↔ string
boolValue, err := strconv.ParseBool("true")
if err != nil {
fmt.Printf("Failed to conv string to bool: %s\n", err)
} else {
fmt.Printf("%T %s conv to %T %v\n", "true", "true", boolValue, boolValue) //string true conv to bool true
}
stringValue = strconv.FormatBool(boolValue)
fmt.Printf("%T %v conv to %T %s\n", boolValue, boolValue, stringValue, stringValue) //bool true conv to string true

go package 学习笔记 —— strconv(string与其他基本数据类型(int, float, bool)的转换)的更多相关文章

  1. Python学习笔记 (2.1)标准数据类型之Number(数字)

    Python3中,数字分为四种——int,float,bool,complex int(整型) 和数学上的整数表示没啥区别,没有大小限制(多棒啊,不用写整数高精了),可正可负.还可表示16进制,以 0 ...

  2. C++string,char* 字符数组,int类型之间的转换

    string.int 常见类型之间相互转换 int & string 之间的转换 C++中更多的是使用流对象来实现类型转换 针对流对象 sstream实现 int,float 类型都可以实现 ...

  3. Redis学习笔记(2)-String

    package cn.com; import java.util.List; import redis.clients.jedis.Jedis; public class Redis_String { ...

  4. JavaScript学习笔记之string

    字符串定义: 1,var myString=“内容”:or var myString=‘内容’ 2,var myString= new String(“内容”)           ---〉创建对象, ...

  5. HTML 学习笔记 JavaScript (String)

    String对象用于存储字符串的数据.这里我们做了JavaScript的String字符串对象常用操作总结. 创建String对象的方式 声明:String 对象的方法也可以在所有基本字符串值中访问到 ...

  6. Thinking in java学习笔记之String的不可变性

    为了提高效率,可以使用StringBuffer或StringBuilder 1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与 ...

  7. Java学习笔记 02 String类、StringBuilder类、字符串格式化和正则表达式

    一.String类一般字符串 声明字符串 >>String str 创建字符串 >>String(char a[])方法用于将一个字符数组创建为String对象 >> ...

  8. C++学习笔记(六):复杂数据类型(string、容器和STL)

    STL 即Standard Template Library(标准模板库),由于C++自带的数据类型过于简单,并不能满足我们的使用需要,而STL作为C++标准的内置库为我们编写好了多种高效的数据结构和 ...

  9. java学习笔记之String类

    String类总结 String类概述: java.lang.String 类是字符串操作类 String类的常用构造方法: //1.直接赋值 String str= "hellojava& ...

随机推荐

  1. OpenCV 使用FLANN进行特征点匹配

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...

  2. 手撸GBDT原理(未完成)

    一直对GBDT里面的具体计算逻辑不太清楚,在网上发现了一篇好博客. 先上总结的关系图 GBDT对类别变量是怎么处理的? 这些东西都是在网上发现的,讲的挺好的. GBDT原理与Sklearn源码分析-回 ...

  3. Zookeeper开源客户端框架Curator的使用

    CuratorFramework Curator框架提供了一套高级的API, 简化了ZooKeeper的操作. 话不多说,看代码 package com.donews.data.util import ...

  4. ZooTracer:打破传统追踪软件的束缚

    编者按:自今年2月24日起,用户可以免费从官网下载Zootracer试用.这是由来自微软剑桥研究院的Joppa和他的同事研发的桌面工具,可追踪任意画质的视频中任意移动物体行踪,是对对视频画质拍摄要求高 ...

  5. 关于分频器的FPGA实现整理思路

    分频器是用的最广的一种FPGA电路了,我最初使用的是crazybingo的一个任意分频器,可以实现高精度任意分频的一个通用模块,他的思想在于首先指定计数器的位宽比如32位,那么这个计数器的最大值就是2 ...

  6. MAVEN实现多环境搭建

    在实际的开发中,会遇到开发环境的不同(开发环境,测试环境,线上环境),会来回根据环境的不同修改配置文件,一不小心修改错误导致无法正常运行,故障排除导致开发效率低.使用maven可以根据环境的不同,自动 ...

  7. zabbix配置文件详解--服务(server)端、客户(agent)端、代理(proxy)端

    在zabbix服务(server)端.客户(agent)端.代理(proxy)端分别对应着一个配置文件,即:zabbix_server.conf,zabbix_agentd.conf,zabbix_p ...

  8. java中的深拷贝

    对象拷贝有时让我们忽视其重要性,又或者因为想当然而导致若干程序问题. 浅拷贝 浅拷贝即普通拷贝,即对要拷贝的对象进行复制.例如对于Entity类: class Entity{ int a; Strin ...

  9. 《软件自动化测试开发-Java和Python测试开发实用指南》出版了

    1.关于书中下载链接的问题:出现404,页面不存在 解答:大小写要区分,l和1不能弄错了 2.关于勘误,出现极个别漏子少字错字 解答:后续版本会改进,目前能理解就好了 ---------------- ...

  10. (为容器分配独立IP方法二)通过虚拟IP实现docker宿主机增加对外IP接口

    虚拟IP.何为虚拟IP,就是一个未分配给真实主机的IP,也就是说对外提供数据库服务器的主机除了有一个真实IP外还有一个虚IP,使用这两个IP中的任意一个都可以连接到这台主机,所有项目中数据库链接一项配 ...