博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang中字符串与数值的转换,整型转浮点型
阅读量:2455 次
发布时间:2019-05-10

本文共 663 字,大约阅读时间需要 2 分钟。

 

#string到intint,err:=strconv.Atoi(string)#string到int64int64, err := strconv.ParseInt(string, 10, 64)#int到stringstring:=strconv.Itoa(int)#int64到stringstring:=strconv.FormatInt(int64,10)

 Float64转int,Int转Unit8

import (	"math"	"fmt")func Uint8FromInt(n int) (uint8, error) {	if 0 <= n && n <= math.MaxUint8 { // conversion is safe		return uint8(n), nil	}	return 0, fmt.Errorf("%d is out of the uint8 range", n)}func IntFromFloat64(x float64) int {	if math.MinInt32 <= x && x <= math.MaxInt32 { // x lies in the integer range		whole, fraction := math.Modf(x)		if fraction >= 0.5 {			whole++		}		return int(whole)	}	panic(fmt.Sprintf("%g is out of the int32 range", x))}

 

转载地址:http://ftdhb.baihongyu.com/

你可能感兴趣的文章
java jnlp_Java SE 11删除JNLP的更好解决方案
查看>>
devops 中台_DevOps中的门控生产
查看>>
keil 开源替代_您需要替代开源的哪些专有工具?
查看>>
总论点和分论点_将破坏性的论点变成富有成效的对话
查看>>
pythonic_使用Pythonic在Python中以图形方式编程
查看>>
python black_格式化Python,但您喜欢使用Black
查看>>
如何在Mac上为Python设置虚拟环境
查看>>
使用Python在GitHub Pages上运行博客
查看>>
如何使用Python和Apache Spark分析日志数据
查看>>
移动端仿钉钉聊天 git_使用Git作为聊天的后端
查看>>
raspberry pi_PiFlash入门:在Linux上启动Raspberry Pi
查看>>
固态硬盘损坏数据如何挽救_开放数据和工具如何在灾难期间挽救生命
查看>>
raspberry_您最老的Raspberry Pi多大了?
查看>>
vscode构建rust_使用rust-vmm构建未来的虚拟化堆栈
查看>>
joplin_介绍Joplin,这是Evernote的开源替代方案
查看>>
使用Pygame模块使用Python构建游戏框架
查看>>
如何使用PostgreSQL简化Python代码
查看>>
软件博览会上的致辞_本地制造商博览会上有4个著名的开源项目
查看>>
pygame游戏角色旋转_使用Pygame移动游戏角色
查看>>
为什么Python和Pygame是入门程序员的最佳选择
查看>>