字符类型

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类型转基本数据类型

最新文章

  1. Java实现Oracle数据库备份
  2. CGAL4.1在VS2010上配置
  3. 黄聪:Mysql数据库还原备份提示MySQL server has gone away 的解决方法(备份文件数据过大)
  4. java边界布局管理器
  5. 标准管道(popen)
  6. 【leetcode】Find Peak Element ☆
  7. SCOI2015题解 &amp;&amp; 考试小结
  8. 【最短路】FOJ 2243 Daxia like uber
  9. Tomcat安装阿里云免费证书
  10. c/c++处理参数
  11. 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)
  12. Linux学习(1)-常用快捷键、文件管理和查询
  13. Vim编辑器的注释,解注,删除与恢复
  14. ElasticSearch(2)---SpringBoot整合ElasticSearch
  15. Codeforces Round 1153(div. 2)
  16. php三级联动(html,php两个页面)
  17. python联系题1
  18. org.apache.catalina.LifecycleException异常的处理
  19. 20172324《Java程序设计》第二周学习总结
  20. 【Android】使用Pull生成/解析XML文件

热门文章

  1. yolo-windows检测高分辨率图像及封装应用
  2. 通过js 存取cookie
  3. Shell 语法之信号与作业
  4. 60行JavaScript代码俄罗斯方块
  5. Commons BeanUtils工具包
  6. [Day6] Nginx 进阶模块
  7. 使用powerDesigner绘制类图
  8. GVEdit中使用graphviz
  9. Luogu P1979 华容道(bfs+最短路)
  10. 生成中国地区随机IP