fork(1) download
  1. # 方法1:round函数
  2. num = 3.1415926
  3. result = round(num, 2)
  4. print(result) # 输出:3.14
  5.  
  6. # 方法2:格式化字符串
  7. result = f"{num:.2f}"
  8. print(result) # 输出:3.14(字符串类型)
  9. print(type(result)) # 输出:
  10.  
  11. float_result = float(f"{num:.2f}") # 转为浮点数
  12. print(float_result) # 输出:3.14
  13.  
  14.  
Success #stdin #stdout 0.09s 14112KB
stdin
Standard input is empty
stdout
3.14
3.14
<class 'str'>
3.14