专业的编程技术博客社区

网站首页 > 博客文章 正文

golang 字符串转换 strconv包(golang字符转数字)

baijin 2024-08-17 10:55:20 博客文章 9 ℃ 0 评论

strconv包

Append

Append 系列函数将整数等转换为字符串后, 添加到现有的字节数组中。

func AppendInt

func AppendInt(dst []byte, i int64, base int) []byte

等价于append(dst, FormatInt(I, base)...)

func AppendBool

func AppendBool(dst []byte, b bool) []byte

等价于append(dst, FormatBool(b)...)

func AppendQuote

func AppendQuote(dst []byte, s string) []byte

等价于append(dst, Quote(s)...)

func AppendQuoteRune

func AppendQuoteRune(dst []byte, r rune) []byte

等价于append(dst, QuoteRune(r)...)

str := make([]byte, 0, 100)
str = strconv.AppendInt(str, 4567, 10)
str = strconv.AppendBool(str, false)
str = strconv.AppendQuote(str, "abcdefg")
str = strconv.AppendQuoteRune(str, '单')
fmt.Println(string(str)) //4567false"abcdefg"'单'
fmt.Println(str) //[52 53 54 55 102 97 108 115 101 34 97 98 99 100 101 102 103 34 39 229 141 149 39]


Format

Format 系列函数把其他类型的转换为字符串

func FormatBool

func FormatBool(b bool) string

根据b的值返回"true"或"false"。

func FormatInt

func FormatInt(i int64, base int) string

返回i的base进制的字符串表示。base 必须在2到36之间, 结果中会使用小写字母'a'到'z'表示大于10的数字。

func FormatUint

func FormatUint(i uint64, base int) string

是FormatInt的无符号整数版本。

func Itoa

func Itoa(i int) string

Itoa是FormatInt(i, 10) 的简写。

func FormatFloat

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

函数将浮点数表示为字符串并返回。

bitSize表示f的来源类型(32:float32、64:float64), 会据此进行舍入。

fmt表示格式:'f'(-ddd.dddd)、'b'(-ddddp±ddd, 指数为二进制)、'e'(-d.dddde±dd, 十进制指数)、'E'(-d.ddddE±dd,十进制指数)、

'g'(指数很大时用'e'格式, 否则'f'格式)、'G'(指数很大时用'E'格式, 否则'f'格式)。

prec控制精度(排除指数部分):对'f'、'e'、'E', 它表示小数点后的数字个数;对'g'、'G', 它控制总的数字个数。

如果prec 为-1, 则代表使用最少数量的、但又必需的数字来表示f。

a := strconv.FormatBool(false)
b := strconv.FormatInt(1234, 10)
c := strconv.FormatUint(12345, 10) //返回10进制的字符串
d := strconv.Itoa(1023) //Itoa是FormatInt(i, 10) 的简写。
f := strconv.FormatFloat(3.141592654, 'f', 4, 64)
fmt.Println(a, b, c, d, f) //false 1234 12345 1023 3.1416

Parse

Parse 系列函数把字符串转换为其他类型

func ParseBool

func ParseBool(str string) (value bool, err error)

返回字符串表示的bool值。

func ParseFloat

func ParseFloat(s string, bitSize int) (f float64, err error)

解析一个表示浮点数的字符串并返回其值。

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

返回字符串表示的整数值, 接受正负号。

func ParseUint

func ParseUint(s string, base int, bitSize int) (n uint64, err error)

ParseUint类似ParseInt但不接受正负号, 用于无符号整型。

func Atoi

func Atoi(s string) (i int, err error)

Atoi是ParseInt(s, 10, 0)的简写。

package main
import (
    "fmt"
    "strconv"
)
func checkError(e error) {
    if e != nil {
    		fmt.Println(e)
    }
}
func main() {
    a, err := strconv.ParseBool("false")
    checkError(err)
    b, err := strconv.ParseFloat("123.23", 64)
    checkError(err)
    c, err := strconv.ParseInt("1234", 10, 64)
    checkError(err)
    d, err := strconv.ParseUint("12345", 10, 64)
    checkError(err)
    e, err := strconv.Atoi("1023")
    checkError(err)
    fmt.Println(a, b, c, d, e) //false 123.23 1234 12345 1023
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表