golang语言渐入佳境[27]-其他类型转string函数

其他类型转string函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import (
"fmt"
"strconv"
)

/*
1、func Itoa(i int) string
Itoa 是 FormatInt(int64(i), 10) 的缩写。

2、func FormatInt(i int64, base int) string
FormatInt 返回给定基数中的i的字符串表示,对于2 <= base <= 36.结果对于数字值> = 10使用小写字母 'a' 到 'z' 。

3、func FormatUint(i uint64, base int) string
FormatUint 返回给定基数中的 i 的字符串表示,对于2 <= base <= 36.结果对于数字值> = 10使用小写字母 'a' 到 'z' 。

4、func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat 根据格式 fmt 和 precision prec 将浮点数f转换为字符串。它将结果进行四舍五入,假设原始数据是从 bitSize 位的浮点值获得的(float32为32,float64为64)。
格式 fmt 是 'b','e','E','f','g'或 'G'。

5、func FormatBool(b bool) string
FormatBool 根据 b 的值返回“true”或“false”
*/

func main() {
TestItoa()

TestFormatInt()

TestFormatUint()

TestFormatFloat()

TestFormatBool()


}

func TestItoa() {
s := strconv.Itoa(199)
fmt.Printf("%T , %v  , 长度:%d \n", s, s, len(s))
fmt.Println("----------------")
}

func TestFormatInt() {
s := strconv.FormatInt(-19968, 16)//4e00
s = strconv.FormatInt(-40869, 16)//9fa5
fmt.Printf("%T , %v  , 长度:%d \n", s, s, len(s))
fmt.Println("----------------")
}

func TestFormatUint() {
s := strconv.FormatUint(19968, 16)//4e00
s = strconv.FormatUint(40869, 16)//9fa5
fmt.Printf("%T , %v  , 长度:%d \n", s, s, len(s))
fmt.Println("----------------")
}

func TestFormatFloat() {
s := strconv.FormatFloat(3.1415926 , 'g' , -1 , 64)
fmt.Printf("%T , %v  , 长度:%d \n", s, s, len(s))
fmt.Println("----------------")
}

func TestFormatBool() {
s := strconv.FormatBool(true)
s = strconv.FormatBool(false)
fmt.Printf("%T , %v  , 长度:%d \n", s, s, len(s))
fmt.Println("----------------")
}

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2329142