当前位置:网站首页>8 - Format integers and floating point numbers

8 - Format integers and floating point numbers

2022-06-24 13:24:00 ruochen

Integer formatting

  • Please format an integer , Press 10 An output , Insufficient 10 Fill in the front 0
n = 1234

print(format(n, '10d'))
#  Zero filling on the left side 
print(format(n, '0>10d'))
#  Right zero padding 
print(format(n, '0<10d'))
      1234
0000001234
1234000000

Floating point format

  • Format a floating point number , Keep two decimal places
x1 = 1234.5678
x2 = 58.1

#  Keep two decimal places 
print(format(x1, '0.2f'))
print(format(x2, '0.2f'))
1234.57
58.10

3. Please describe format The main usage of function

# format  The function is used to format a numeric value , Specify the formatting rule through the second parameter 

#  Right alignment 
print(format(x2, '*>12.2f'))

#  Align left 
print(format(x2, '*<12.2f'))

#  Center alignment 
print(format(x2, '*^12.2f'))

#  Divide... With thousands 
print(format(123455678, ','))

#  Use... For integers ',' Division , And keep two decimal places 
print(format(123456.12321, ',.2f'))

#  Output according to scientific counting method 
print(format(x1, 'e'))

#  Keep two decimal places , Scientific counting outputs 
print(format(x1, '0.2E'))
*******58.10
58.10*******
***58.10****
123,455,678
123,456.12
1.234568e+03
1.23E+03
原网站

版权声明
本文为[ruochen]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210522180818071W.html