当前位置:网站首页>基本数据类型和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)
}
边栏推荐
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- Automatic upgrading of database structure in rainbow
- Obsidan之数学公式的输入
- grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
- Famine cloud service management script
- Offer harvester: add and sum two long string numbers (classic interview algorithm question)
- MES系统,是企业生产的必要选择
- Rainbond结合NeuVector实践容器安全管理
- Implementation method of data platform landing
- SSM 整合
猜你喜欢
单元测试报告成功率低
Splunk查询csv lookup table数据动态查询
归并排序和非比较排序
Obsidan之数学公式的输入
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Installation and configuration of PLSQL
[untitled]
Splunk query CSV lookup table data dynamic query
BiSeNet的特点
Easy to understand SSO
随机推荐
Opencv learning note 4 - expansion / corrosion / open operation / close operation
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Open3D ISS关键点
2-3查找樹
Installation and configuration of PLSQL
解析机器人科技发展观对社会研究论
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Full text query classification
Réplication de vulnérabilité - désrialisation fastjson
Ebpf cilium practice (1) - team based network isolation
Use of any superclass and generic extension function in kotlin
Infix keyword infix expression and the use of generic extension function in kotlin
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
在Rainbond中一键部署高可用 EMQX 集群
Deit learning notes
Practice of implementing cloud native Devops based on rainbow library app
Merge sort and non comparison sort
Splunk中single value视图使用将数值替换为文字
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Opencv learning notes II - basic image operations