立即學習: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"))