当前位置:网站首页>基本数据类型和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)
}
边栏推荐
- MES system is a necessary choice for enterprise production
- Implementation method of data platform landing
- Kotlin combines flatmap for filtering and zip merge operators
- [kuangbin]专题十五 数位DP
- Bisenet features
- 数据中台落地实施之法
- BiSeNet的特点
- opencv学习笔记五——梯度计算/边缘检测
- Train your dataset with swinunet
- National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
猜你喜欢

一文了解如何源码编译Rainbond基础组件

单场带货涨粉10万,农村主播竟将男装卖爆单?

在Rainbond中一键部署高可用 EMQX 集群

Lua programming learning notes

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

Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu

BiSeNet的特點

利用 Helm 在各类 Kubernetes 中安装 Rainbond

Input of mathematical formula of obsidan

柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
随机推荐
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
eBPF Cilium实战(1) - 基于团队的网络隔离
DeiT学习笔记
Vulnerability recurrence fastjson deserialization
BiSeNet的特點
Interface as a parameter (interface callback)
The use of generics and vararg variable parameters in kotlin
OpenVSCode云端IDE加入Rainbond一体化开发体系
Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
Opencv learning note 3 - image smoothing / denoising
One click installation of highly available Nacos clusters in rainbow
rsync远程同步
Rainbond结合NeuVector实践容器安全管理
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
Opencv learning note 4 - expansion / corrosion / open operation / close operation
Splunk query CSV lookup table data dynamic query
Opencv learning notes 1 -- several methods of reading images