Commit 991c96e7 by 陈森彬

去掉小数点后多余的0

parent a546d8bf
Showing with 12 additions and 4 deletions
......@@ -106,21 +106,29 @@ def unit_conversion(unit_res, kw):
if "/" not in kw: # 单独处理除号
if "%" in kw: # 百分号的单独处理
unit_num = int(kw.replace(unit_str, ""))
true_unit = str(unit_num * unit_data[unit_str])
true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str]))
elif "." in kw:
unit_num = float(kw.replace(unit_str, ""))
true_unit = str(unit_num * unit_data[unit_str]) + unit_key
true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str])) + unit_key
else:
unit_num = int(kw.replace(unit_str, ""))
true_unit = str(unit_num * unit_data[unit_str]) + unit_key
true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str])) + unit_key
else:
num_list = kw.replace(unit_str, "").split("/")
unit_num = round_up(int(num_list[0]) / int(num_list[1]))
true_unit = str(unit_num * unit_data[unit_str]) + unit_key
true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str])) + unit_key
return true_unit
return None
def delete_extra_zero(n):
'''删除小数点后多余的0'''
if isinstance(n, int):
return n
if isinstance(n, float):
n = str(n).rstrip('0') # 删除小数点后多余的0
n = int(n.rstrip('.')) if n.endswith('.') else float(n) # 只剩小数点直接转int,否则转回float
return n
def round_up(value):
return round(value * 1000) / 1000.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment