Files
Python_CookBook_repo/3.数字日期和时间/3.对数值做格式化输出.py
2025-09-10 16:12:45 +08:00

15 lines
441 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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'))