Go语言string,int,int64 ,float之间类型转换方法

(1)int转string

s := strconv.Itoa(i)
等价于s := strconv.FormatInt(int64(i), 10)

(2)int64转string

i := int64(123)
s := strconv.FormatInt(i, 10)

第二个参数为基数,可选2~36

注:对于无符号整形,可以使用FormatUint(i uint64, base int)

(3)string转int

i, err := strconv.Atoi(s)

(4)string转int64

i, err := strconv.ParseInt(s, 10, 64)

第二个参数为基数(2~36),第三个参数位大小表示期望转换的结果类型,其值可以为0, 8, 16, 32和64,分别对应 int, int8, int16, int32和int64

(5)float相关

float转string:

v := 3.1415926535
s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E', -1, 64)//float64

函数原型及参数含义具体可查看:https://golang.org/pkg/strconv/#FormatFloat

string转float:

s := "3.1415926535"
v1, err := strconv.ParseFloat(v, 32)
v2, err := strconv.ParseFloat(v, 64)

 PS:go语言string、int、int64互相转换

//string到int
int,err:=strconv.Atoi(string)
//string到int64
int64, err := strconv.ParseInt(string, 10, 64)
//int到string
string:=strconv.Itoa(int)
//int64到string
string:=strconv.FormatInt(int64,10)
//string到float32(float64)
float,err := strconv.ParseFloat(string,32/64)
//float到string
string := strconv.FormatFloat(float32, 'E', -1, 32)
string := strconv.FormatFloat(float64, 'E', -1, 64)
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)

总结

以上所述是小编给大家介绍的Go语言string,int,int64 ,float之间类型转换方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • mongodb exception: $concat only supports strings, not NumberInt32解决办法

    今天在用mongodb操作aggregation的时候出现这个问题,我是想格式化日期,例如"2013-10-17 04:41:37 UTC"变成"10月17日", 复制代码 代码如下: 'fdate' => { '$concat' => ['$date.month', '月', '$date.day', '日'] } 出现 exception: $concat only supports strings, not NumberInt32 原来$conca

  • mongodb错误tcmalloc: large alloc out of memory, printing stack and exiting解决办法

    最近Mongodb会经常突然挂掉,检查日志发现如下的错误: 复制代码 代码如下: tcmalloc: large alloc 2061584302080 bytes == (nil) @ Tue Nov 26 17:45:04.539 out of memory, printing stack and exiting: 0xdddd81 0x6cfb4e 0x121021d 0xafcc1f 0xaf815f 0xaf8d1d 0xaf8e0f 0xaf52ae 0xaf53c9 0xb1eb1

  • go语言中的interface使用实例

    go语言中的interface是一组未实现的方法的集合,如果某个对象实现了接口中的所有方法,那么此对象就实现了此接口.与其它面向对象语言不同的是,go中无需显示声明调用了哪个接口. 复制代码 代码如下: package main   import (  "fmt" )   type I interface {  Get() int  Put(int) }   type S struct{ i int }   func (p *S) Get() int  { return p.i } f

  • golang实现unicode转换为字符串string的方法

    本文实例讲述了golang实现unicode转换为字符串string的方法.分享给大家供大家参考,具体如下: 复制代码 代码如下: package main import (     "bytes"     "encoding/binary"     "encoding/hex"     "fmt"     "strings" ) func main() {     str := `\u5bb6\u65cf

  • Go语言interface详解

    interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单的说,interface是一组method的组合,我们通过interface来定义对象的一组行为. 我们前面一章最后一个例子中Student和Employee都能SayHi,虽然他们的内部实现不一样,但是那不重要,重要的是他们都能say hi 让我们来继续做更多的扩展,Student和Employe

  • Go语言string,int,int64 ,float之间类型转换方法

    (1)int转string s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := int64(123) s := strconv.FormatInt(i, 10) 第二个参数为基数,可选2~36 注:对于无符号整形,可以使用FormatUint(i uint64, base int) (3)string转int i, err := strconv.Atoi(s) (4)string转in

  • QT中QByteArray与char、int、float之间的互相转化

    目录 1.问题来源 2.QByteArray与char*的转换 2.1 QByteArray 转 char* 2.2 char* 转 QByteArray 3.QByteArray与int 以及int[] 的转换 3.1. int 与 QByteArray 互转 3.2. int[] 与 QByteArray 互转 4.QByteArray与float 以及float[] 的转换 4.1. float[] 与 QByteArray 互转 4.2. float 与 QByteArray 互转 1.

  • Go语言中int、float、string类型之间相互的转换

    目录 前言 整形转字符串 fmt.Sprintf 使用方法 strconv.Itoa 使用方法 strconv.FormatInt 入参 使用方法 浮点型转字符串 fmt.Sprintf 入参 使用方法 字符串转整形 strconv.Atoi 使用方法 strconv.ParseInt 使用方法 字符串转浮点型 strconv.ParseFloat 使用方法 总结 前言 Go 开发中经常设计到类型转换,本文就重点记录下 整形.浮点型和字符串类型互相转换的方法. 整形转字符串 fmt.Sprint

  • C语言中字符串与各数值类型之间的转换方法

    C语言的算法设计中,经常会需要用到字符串,而由于c语言中字符串并不是一个默认类型,其标准库stdlib设计了很多函数方便我们处理字符串与其他数值类型之间的转换. 首先放上一段展示各函数使用的代码,大家也可以copy到自己的机器上运行观察 #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int num=183; char str[3]; //itoa函数将整型转换为字符串数值类型 it

  • go中string、int、float相互转换的实现示例

    目录 string转其它 string --> int:将字符串13转换为int类型的数值13 string --> int64:将字符串13转换为int64(十进制) string --> float64.float32 int.int64转其它 int --> string int64 --> string float转其它 float --> string float64 --> int64(会有精度损失) 之前很多次用到的时候总会忘一些,这次记下来,后面再

  • 浅谈python 四种数值类型(int,long,float,complex)

    Python支持四种不同的数值类型,包括int(整数)long(长整数)float(浮点实际值)complex (复数),本文章向码农介绍python 四种数值类型,需要的朋友可以参考一下. 数字数据类型存储数值.他们是不可改变的数据类型,这意味着改变数字数据类型的结果,在一个新分配的对象的值. Number对象被创建,当你给他们指派一个值.例如: var1 = 1 var2 = 10 您也可以删除数字对象的参考,使用del语句. del语句的语法是: del var1[,var2[,var3[

  • C#与C++之间类型的对应知识点总结

    Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUBLE Double DWORD Int32 or UInt32 FLOAT Single HANDLE (and all other handle types, such as HFONT and HMENU) IntPtr, UintPtr or HandleRef HRESULT Int32 o

  • Java基本类型与byte数组之间相互转换方法

    Java基本类型与byte数组之间相互转换,刚刚写的 package cn.teaey.utils; import java.nio.charset.Charset; public class ByteUtil { public static byte[] getBytes(short data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00)

  • C语言数组和指针,内存之间的关系

    首先论证一维数组和一级指针之前的关系,我们常常使用一级指针指针的方式访问一维数组,只有对内存的理解到位才能理解它们直接的关系. 1.数组名是数组的首地址 2.对数组名取地址得到的还是数组的首地址 3.数组的访问方式其实就是首地址+偏移的寻址访问 我们在程序中会定义很多变量,有基本类型和自定义类型在进行开发的时候我对内存的访问访问就是通过变量名赋值的方式读写内存但是如果你看到的直接变量的符号名你将不可能理解内存.每一种类型都有字节宽度,char 1字节 short 2字节 int 字节float

  • Go语言使用sort包对任意类型元素的集合进行排序的方法

    本文实例讲述了Go语言使用sort包对任意类型元素的集合进行排序的方法.分享给大家供大家参考.具体如下: 使用sort包的函数进行排序时,集合需要实现sort.Inteface接口,该接口中有三个方法: 复制代码 代码如下: // Len is the number of elements in the collection.  Len() int  // Less reports whether the element with  // index i should sort before t

随机推荐