學習筆記(07):Python 面試100講(基於Python3.x)-格式化整數和浮點數

立即學習:https://edu.csdn.net/course/play/26755/340116?utm_source=blogtoedu

格式化輸出整數和浮點數

 

# 整數
n = 1234
#       1234 10位
print(format(n, '10d'))
# 0000001234 右對齊
print(format(n, '0>10d'))
# 1234000000 左對齊
print(format(n, '0<10d'))


# 浮點數
x1 = 1233.456789
x2 = 30.1
# 1233.46 2位小數
print(format(x1, '0.2f'))
# 30.10 2位小數
print(format(x2, '0.2f'))

# ********30.1000 右對齊
print(format(x2, "*>15.4f"))
# 30.1000******** 左對齊
print(format(x2, "*<15.4f"))
# ****30.1000**** 中心對奇
print(format(x2, "*^15.4f"))
# 1,233.456789 3位,
print(format(x1, ","))
# 1,233.46 3位,以及2位小數
print(format(x1, ",.2f"))
# 1.233457e+03 科學技術法
print(format(x1, "e"))

 

發佈了77 篇原創文章 · 獲贊 2 · 訪問量 7078
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章