• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
golang数据类型二

 

 

 

 

 

 

 

字符类型

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 3.14基本数据类型的相互转换

 

 

 

 

 

3.15基本数据类型和string的转换

 

 

 

 

 

 

FormatInt

// FormatUint 将 int 型整数 i 转换为字符串形式
// base:进位制(2 进制到 36 进制)
// 大于 10 进制的数,返回值使用小写字母 'a' 到 'z'
func FormatInt(i int64, base int) string

func main(){
	var num1 int = 99
	//var num2 float64 = 23.456
	//var b bool = true
	var str string

	str = strconv.FormatInt(int64(num1),2)
	fmt.Printf("str type %T  str=%q\n",str,str)

}

  返回:

str type string  str="1100011"

  

FormatFloat 

/ FormatFloat 将浮点数 f 转换为字符串值
// f:要转换的浮点数
// fmt:格式标记(b、e、E、f、g、G)
// prec:精度(数字部分的长度,不包括指数部分)
// bitSize:指定浮点类型(32:float32、64:float64)
//
// 格式标记:
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)
//
// 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数
// 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分)
func FormatFloat(f float64, fmt byte, prec, bitSize int) string

func main() {
	f := 100.12345678901234567890123456789
	fmt.Println(strconv.FormatFloat(f, 'b', 5, 32))
	// 13123382p-17
	fmt.Println(strconv.FormatFloat(f, 'e', 5, 32))
	// 1.00123e+02
	fmt.Println(strconv.FormatFloat(f, 'E', 5, 32))
	// 1.00123E+02
	fmt.Println(strconv.FormatFloat(f, 'f', 5, 32))
	// 100.12346
	fmt.Println(strconv.FormatFloat(f, 'g', 5, 32))
	// 100.12
	fmt.Println(strconv.FormatFloat(f, 'G', 5, 32))
	// 100.12
	fmt.Println(strconv.FormatFloat(f, 'b', 30, 32))
	// 13123382p-17
	fmt.Println(strconv.FormatFloat(f, 'e', 30, 32))
	// 1.001234588623046875000000000000e+02
	fmt.Println(strconv.FormatFloat(f, 'E', 30, 32))
	// 1.001234588623046875000000000000E+02
	fmt.Println(strconv.FormatFloat(f, 'f', 30, 32))
	// 100.123458862304687500000000000000
	fmt.Println(strconv.FormatFloat(f, 'g', 30, 32))
	// 100.1234588623046875
	fmt.Println(strconv.FormatFloat(f, 'G', 30, 32))
	// 100.1234588623046875
}

  

FormatBool 

// FormatBool 将布尔值转换为字符串 "true" 或 "false"
func FormatBool(b bool) string

func main() {
	fmt.Println(strconv.FormatBool(0 < 1)) // true
	fmt.Println(strconv.FormatBool(0 > 1)) // false
}

  

func FormatInt

func FormatInt(i int64, base int) string

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

// Itoa 相当于 FormatInt(i, 10)
func Itoa(i int) string

func main() {
	fmt.Println(strconv.Itoa(-2048)) // -2048
	fmt.Println(strconv.Itoa(2048))  // 2048
}

  

string类型转基本数据类型

 

本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/10988972.html

posted on 2019-06-07 19:17  孙龙-程序员  阅读(245)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3