当前位置:网站首页>Go strconv package

Go strconv package

2022-06-09 10:19:00 General_ zy

const

const IntSize = intSize

intSize yes int or uint Number of words of type .

func ParseBool

func ParseBool(str string) (value bool, err error)
  1. Incoming representation bool String , Such as :1,0,t
  2. return bool type

func ParseInt

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

bin23:=strconv.FormatInt(int64(23),2)
parseInt, err := strconv.ParseInt(bin23, 2, strconv.IntSize)
if err != nil {
    
	return
}
fmt.Println(parseInt)
// 23
  1. Incoming data can be converted to int String

  2. base Specifies the base number represented by the string (2 To 36), If base by 0, Will be judged from the string preposition ,"0x" yes 16 Base number ,"0" yes 8 Base number , It is 10 Base number ;

  3. bitSize Of the expected value bit size , Used for upper limit of value , The final return is int64 type ,0、8、16、32、64 Represent the int、int8、int16、int32、int64;

func ParseUint

func ParseUint(s string, base int, bitSize int) (n uint64, err error)
  1. Pass in a string representing a positive integer
  2. Others and ParseInt identical

func ParseFloat

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

Parses a string representing a floating-point number and returns its value .

func FormatBool

func FormatBool(b bool) string

take bool Value to string

func FormatInt

func FormatInt(i int64, base int) string

fmt.Println(strconv.FormatInt(int64(23),2))
// 10111
  1. Pass in int64 And a base number
  2. Returns the string after converting the number according to the decimal

func FormatUint

func FormatUint(i uint64, base int) string
  1. Positive integer
  2. The rest is the same as above

func FormatFloat

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

fmt.Println(strconv.FormatFloat(float64(32),'f',10,64))
//32.0000000000
  1. Function represents a floating-point number as a string and returns .

  2. fmt Set the format of the return value :‘f’(-ddd.dddd)、‘b’(-ddddp±ddd, The exponent is binary )、‘e’(-d.dddde±dd, Decimal index )、‘E’(-d.ddddE±dd, Decimal index )、‘g’( It's used when the exponent is large ’e’ Format , otherwise ’f’ Format )、‘G’( It's used when the exponent is large ’E’ Format , otherwise ’f’ Format ).

  3. prec Control accuracy ( Exclusion index part ): Yes ’f’、‘e’、‘E’, It represents the number of Numbers behind the decimal point ; Yes ’g’、‘G’, It controls the total number of Numbers . If prec by -1, Is for using the least amount 、 But the necessary Numbers f.

  4. bitSize Express f Source type (32:float32、64:float64), I'm going to round that .

func Atoi

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

Atoi yes ParseInt(s, 10, 0) Abbreviation .

func Itoa

func Itoa(i int) string

Itoa yes FormatInt(i, 10) Abbreviation .

func AppendBoo

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

tmp:=make([]byte,1,10)
tmp[0]='1'
fmt.Println(strconv.AppendBool(tmp,true))
fmt.Println(string(strconv.AppendBool(tmp,true)))
//[49 116 114 117 101]
//1true

Equivalent to append(dst, FormatBool(b)...) take bool Append to byte slice

func AppendInt

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

tmp:=make([]byte,1,10)
tmp[0]='1'
fmt.Println(strconv.AppendInt(tmp,23,2))
fmt.Println(string(strconv.AppendInt(tmp,23,2)))
// [49 49 48 49 49 49]
// 110111

Equivalent to append(dst, FormatInt(I, base)...)

func AppendUint

func AppendUint(dst []byte, i uint64, base int) []byte

Equivalent to append(dst, FormatUint(I, base)...)

func AppendFloat

func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte

Equivalent to append(dst, FormatFloat(f, fmt, prec, bitSize)...)

原网站

版权声明
本文为[General_ zy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090938358233.html