当前位置:网站首页>Basic data types and string types are converted to each other
Basic data types and string types are converted to each other
2022-07-07 08:32:00 【Jimmy_ jimi】
Basic data type string
Method 1 :fmt.Sprintf format
import "fmt"
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
//int To string
str := fmt.Sprintf("%d", num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 To string
str = fmt.Sprintf("%f", num2)
fmt.Printf("str type : %T , str=%q", str, str)
//bool To string
str = fmt.Sprintf("%t", b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte To 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"
Method 2 :strconv Function of the package
import (
"fmt"
"strconv"
)
func main() {
var num1 int = 99
var num2 float64 = 123.12
var b bool = true
var myChar byte = 'h'
// Mode one 、int To string
str := strconv.FormatInt(int64(num1), 10)
fmt.Printf("str type : %T , str=%q", str, str)
// Mode two 、int To string
str = strconv.Itoa(num1)
fmt.Printf("str type : %T , str=%q", str, str)
//float64 To string
str = strconv.FormatFloat(num2, 'f', 10, 64)
fmt.Printf("str type : %T , str=%q", str, str)
//bool To string
str = strconv.FormatBool(b)
fmt.Printf("str type : %T , str=%q", str, str)
//byte To 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 Type to basic data type
package main
import (
"fmt"
"strconv"
)
func main() {
var num1 string = "99"
var num2 string = "123.12"
var b string = "true"
// string To int64
//10 Represents the hexadecimal after output ,2-32 Base number
// here 64 Is the limit bit , Not the data type of the specified output , in fact strconv.ParseInt Only return int64 Type data ,bitesize The limit is the data size , If the transformed shaping data num1 exceed 127, that err It will show that the data is out of range ( Report errors (variable of type int64) as type int in argument to fbn Cause analysis ).
str, _ := strconv.ParseInt(num1, 10, 64)
fmt.Printf("str type: %T str=%d", str, str)
// string To float64
number, _ := strconv.ParseFloat(num2, 64)
fmt.Printf("str type: %T str=%f", number, number)
// string To bool
ret, _ := strconv.ParseBool(b)
fmt.Printf("str type: %T str=%t", ret, ret)
}
边栏推荐
- The reified keyword in kotlin is used for generics
- 利用 Helm 在各类 Kubernetes 中安装 Rainbond
- JS copy picture to clipboard read clipboard
- opencv学习笔记五——梯度计算/边缘检测
- 下载和安装orcale database11.2.0.4
- Bisenet features
- 发挥创客教育空间的广泛实用性
- Give full play to the wide practicality of maker education space
- JEditableTable的使用技巧
- 2-3 lookup tree
猜你喜欢

Lua 编程学习笔记

Input and output of floating point data (C language)

Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
![[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)](/img/dc/a809f2ec5f9e85a02e68c88bdcdb5e.png)
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)

Analyzing the influence of robot science and technology development concept on Social Research

Lua programming learning notes

Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications

探索STEAM艺术设计中的创造力

In go language, function is a type

Famine cloud service management script
随机推荐
Le système mes est un choix nécessaire pour la production de l'entreprise
BiSeNet的特點
AVL平衡二叉搜索树
2-3 lookup tree
Famine cloud service management script
Function extension, attribute extension and non empty type extension in kotlin
在Rainbond中一键部署高可用 EMQX 集群
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
DeiT学习笔记
解析机器人科技发展观对社会研究论
go写一个在一定时间内运行的程序
Go语言中,函数是一种类型
Openvscode cloud ide joins rainbow integrated development system
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
[machine learning] watermelon book data set_ data sharing
[untitled]
[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Infix keyword infix expression and the use of generic extension function in kotlin
It's too true. There's a reason why I haven't been rich