Commit 57478123 by 陈森彬

优化中文参数带小数点,除号等

parent 991c96e7
Showing with 13 additions and 4 deletions
...@@ -84,11 +84,18 @@ def word_conversion(unit_res, kw): ...@@ -84,11 +84,18 @@ def word_conversion(unit_res, kw):
if unit_str in cast_map: if unit_str in cast_map:
for unit_key, unit_data in unit_map.items(): for unit_key, unit_data in unit_map.items():
if cast_map[unit_str] in unit_data: if cast_map[unit_str] in unit_data:
if unit_data[cast_map[unit_str]] == 1: if "/" not in kw: # 单独处理除号
true_unit = unit_key if "." in kw: # 包含小数点
unit_num = float(kw.replace(unit_str, ""))
true_unit = str(delete_extra_zero(unit_num * unit_data[cast_map[unit_str]])) + unit_key
else: else:
true_unit = str(unit_data[cast_map[unit_str]]) + unit_key unit_num = int(kw.replace(unit_str, ""))
return kw.replace(unit_str, true_unit) true_unit = str(delete_extra_zero(unit_num * unit_data[cast_map[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(delete_extra_zero(unit_num * unit_data[cast_map[unit_str]])) + unit_key
return true_unit
return None return None
...@@ -121,6 +128,7 @@ def unit_conversion(unit_res, kw): ...@@ -121,6 +128,7 @@ def unit_conversion(unit_res, kw):
return None return None
def delete_extra_zero(n): def delete_extra_zero(n):
'''删除小数点后多余的0''' '''删除小数点后多余的0'''
if isinstance(n, int): if isinstance(n, int):
...@@ -130,6 +138,7 @@ def delete_extra_zero(n): ...@@ -130,6 +138,7 @@ def delete_extra_zero(n):
n = int(n.rstrip('.')) if n.endswith('.') else float(n) # 只剩小数点直接转int,否则转回float n = int(n.rstrip('.')) if n.endswith('.') else float(n) # 只剩小数点直接转int,否则转回float
return n return n
def round_up(value): def round_up(value):
return round(value * 1000) / 1000.0 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