当前位置:网站首页>基本数据类型和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)
}
边栏推荐
- Splunk中single value视图使用将数值替换为文字
- One click deployment of highly available emqx clusters in rainbow
- iptables 之 state模块(ftp服务练习)
- Wang Zijian: is the NFT of Tencent magic core worth buying?
- IELTS review progress and method use [daily revision]
- Infix keyword infix expression and the use of generic extension function in kotlin
- Application of slip ring of shipborne radar antenna
- The use of generics and vararg variable parameters in kotlin
- Deit learning notes
- Le système mes est un choix nécessaire pour la production de l'entreprise
猜你喜欢

GFS分布式文件系统

CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand

解析创新教育体系中的创客教育
![[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)](/img/c2/32a2c1ede493b778a6c44077d765d0.png)
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)

Merge sort and non comparison sort

Battery and motor technology have received great attention, but electric control technology is rarely mentioned?

A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)

归并排序和非比较排序

Practice of implementing cloud native Devops based on rainbow library app

Opencv learning note 5 - gradient calculation / edge detection
随机推荐
The use of generics and vararg variable parameters in kotlin
探索STEAM艺术设计中的创造力
Golang 编译约束/条件编译 ( // +build <tags> )
Zcmu--1396: queue problem (2)
opencv学习笔记一——读取图像的几种方法
GFS分布式文件系统
漏洞复现-easy_tornado
Rsync remote synchronization
Opencv learning note 5 - gradient calculation / edge detection
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Xcit learning notes
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Tuowei information uses the cloud native landing practice of rainbow
Ebpf cilium practice (2) - underlying network observability
Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
藏书馆App基于Rainbond实现云原生DevOps的实践
Full text query classification
Lua 编程学习笔记
Le système mes est un choix nécessaire pour la production de l'entreprise
接口作为参数(接口回调)