当前位置:网站首页>基本数据类型和string类型互相转化
基本数据类型和string类型互相转化
2022-07-07 05:27:00 【Jimmy_jimi】
基本数据类型转string
方法一:fmt.Sprintf格式化
import "fmt"
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
//int 转为 string
str := fmt.Sprintf("%d", num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 转为 string
str = fmt.Sprintf("%f", num2)
fmt.Printf("str type : %T , str=%q", str, str)
//bool 转为 string
str = fmt.Sprintf("%t", b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte 转为 string
str = fmt.Sprintf("%c", myChar)
fmt.Printf("str type : %T , str=%q", str, str)
}
// str type : string , str="99"
// str type : string , str="123.120000"
// str type : string , str="true"
// str type : string , str="h"
方法二:strconv包的函数
import (
"fmt"
"strconv"
)
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
//方式一、int 转为 string
str := strconv.FormatInt(int64(num1), 10)
fmt.Printf("str type : %T , str=%q", str, str)
//方式二、int 转为 string
str = strconv.Itoa(num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 转为 string
str = strconv.FormatFloat(num2, 'f', 10, 64)
fmt.Printf("str type : %T , str=%q", str, str)
//bool 转为 string
str = strconv.FormatBool(b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte 转为 string
//str = strconv.Itoa(int(myChar))
str = string(myChar)
fmt.Printf("str type : %T , str=%q", str, str)
}
// str type : string , str="99"
// str type : string , str="99"
// str type : string , str="123.1200000000"
// str type : string , str="true"
// str type : string , str="h"
string类型转基本数据类型
package main
import (
"fmt"
"strconv"
)
func main() {
var num1 string = "99"
var num2 string = "123.12"
var b string = "true"
// string 转为 int64
//10 表示输出后的进制,2-32进制
//这里64为限制位,不是指定输出的数据类型,事实上strconv.ParseInt只能返回int64类型数据,bitesize限制的是数据大小,如果转化后的整形数据num1超过127,那么err就会显示数据超出范围(报错(variable of type int64) as type int in argument to fbn原因分析).
str, _ := strconv.ParseInt(num1, 10, 64)
fmt.Printf("str type: %T str=%d", str, str)
// string 转为 float64
number, _ := strconv.ParseFloat(num2, 64)
fmt.Printf("str type: %T str=%f", number, number)
// string 转为 bool
ret, _ := strconv.ParseBool(b)
fmt.Printf("str type: %T str=%t", ret, ret)
}
边栏推荐
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
- Function extension, attribute extension and non empty type extension in kotlin
- Give full play to the wide practicality of maker education space
- Opencv learning note 5 - gradient calculation / edge detection
- 饥荒云服管理脚本
- 机器人教育在动手实践中的真理
- Lua 编程学习笔记
- IELTS review progress and method use [daily revision]
- 在 Rainbond 中一键安装高可用 Nacos 集群
猜你喜欢
Opencv learning notes II - basic image operations
XCiT学习笔记
Open3d ISS key points
2-3查找樹
Ebpf cilium practice (2) - underlying network observability
机器人教育在动手实践中的真理
Wang Zijian: is the NFT of Tencent magic core worth buying?
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Using helm to install rainbow in various kubernetes
拓维信息使用 Rainbond 的云原生落地实践
随机推荐
2 - 3 arbre de recherche
One click installation of highly available Nacos clusters in rainbow
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
opencv学习笔记五——梯度计算/边缘检测
POJ - 3616 Milking Time(DP+LIS)
Iptables' state module (FTP service exercise)
漏洞复现-easy_tornado
Train your dataset with swinunet
BiSeNet的特點
2-3查找树
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
It's too true. There's a reason why I haven't been rich
[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
IELTS review progress and method use [daily revision]
Golang 编译约束/条件编译 ( // +build <tags> )
下载和安装orcale database11.2.0.4
The reified keyword in kotlin is used for generics
Rainbond结合NeuVector实践容器安全管理
Snyk 依赖性安全漏洞扫描工具