Files
Python_CookBook_repo/3.数字日期和时间/3.对数值做格式化输出.py

15 lines
441 B
Python
Raw Permalink Normal View History

2025-09-10 16:12:45 +08:00
if __name__ == '__main__':
x = 1234.56789
# 对数值格式化输出我们使用经典format函数
print(format(x, '.2f'))
print(format(x, '+<10.2f'))
print(format(x, '=>10.2f'))
print(format(x, '-^10.2f'))
# 有一些特殊的比如数值特有的千位逗号1000
print(format(x, ',.2f'))
# 如果想用科学计数法把f改成e就行
print(format(x, '.2e'))
print(format(x, 'e'))