当前位置:网站首页>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)
}
边栏推荐
- Using helm to install rainbow in various kubernetes
- [quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
- 【雅思口语】安娜口语学习记录 Part2
- MES系统,是企业生产的必要选择
- 一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- Ebpf cilium practice (1) - team based network isolation
- 解析机器人科技发展观对社会研究论
- XCiT学习笔记
- The reified keyword in kotlin is used for generics
猜你喜欢
Exercise arrangement 2.10, 11
Merge sort and non comparison sort
2 - 3 arbre de recherche
Lua programming learning notes
Train your dataset with swinunet
在 Rainbond 中一键安装高可用 Nacos 集群
Open3D ISS关键点
eBPF Cilium实战(2) - 底层网络可观测性
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
[hard core science popularization] working principle of dynamic loop monitoring system
随机推荐
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
如何理解分布式架构和微服务架构呢
饥荒云服管理脚本
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Go语言中,函数是一种类型
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Learn how to compile basic components of rainbow from the source code
Use of out covariance and in inversion in kotlin
Snyk dependency security vulnerability scanning tool
eBPF Cilium实战(2) - 底层网络可观测性
Virtual address space
Understanding of out covariance, in inversion and invariance in kotlin
The single value view in Splunk uses to replace numeric values with text
Several ways of lambda used in functions in kotlin (higher-order functions)
Zcmu--1492: problem d (C language)
MES system is a necessary choice for enterprise production
JS copy picture to clipboard read clipboard