栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

1.8格式化整数和浮点型

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1.8格式化整数和浮点型

学习目标: ①格式化整数 ②格式化浮点数 ③描述format函数
  • Python日常

内容展示: ①:格式化整数
n = 12234
print(format(n, '10d'))   #      12234
print(format(n, '0>10d'))  # 左側   0000012234
print(format(n, '0<10d'))  # 右側   1223400000
②:格式化浮点型
x1 = 1234.5677
x2 = 30.1
print(format(x1, '0.2f'))  # 保留小数点后两位  1234.57
print(format(x2, '.2f'))   # 保留小数点后两位  30.10
③:format函数描述
x1 = 1234.5677
x2 = 30.1
print(format(x2, '*>15.4f')) # 右对齐,********30.1000
print(format(x2, '*<15.4f')) # 左对齐 30.1000********
print(format(x2, '*^15.4f')) # 中心对齐 ****30.1000****
print(format(123456789, ',')) # 用千分号分隔 123,456,789
print(format(123352.76681781, ',.2f')) # 123,352.77

print(format(x1, 'e'))    # 1.234568e+03
print(format(x1, '0.2e'))  # 1.23e+03

总结:

  
右对齐 print(format(s, ‘>15.4f’)) # 15:输出一共十五位 .4:小數点后保留 四位
左对齐 print(format(s, '<15.4f’))
中间对齐 print(format(s, ‘*^15.4f’))
千分号分隔 print(format(s, ‘,’))
千分号加小数点限制 print(format(s, ‘,.2f’))


  • CSDN Python日常 第8 篇
  • 下一篇链接
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号