修复条码处理和数量计算问题:修复条码格式化函数,确保在数量为空时能正确计算

This commit is contained in:
2025-05-30 12:08:06 +08:00
parent 5cf3eeed0f
commit c9afe413f5
2 changed files with 69 additions and 5 deletions
+20 -5
View File
@@ -202,12 +202,27 @@ def format_barcode(barcode: Any) -> str:
Returns:
格式化后的条码字符串
"""
if isinstance(barcode, (int, float)) or is_scientific_notation(str(barcode)):
if barcode is None:
return ""
# 先转为字符串
barcode_str = str(barcode).strip()
# 判断是否为科学计数法
if is_scientific_notation(barcode_str):
try:
# 转换为整数并格式化为字符串
return f"{int(float(barcode))}"
# 科学计数法转为普通数字字符串
barcode_str = f"{float(barcode_str):.0f}"
except (ValueError, TypeError):
pass
# 如果不是数字或转换失败,返回原始字符串
return str(barcode)
# 移除可能的小数部分(如"123456.0"变为"123456"
if '.' in barcode_str:
barcode_str = re.sub(r'\.0+$', '', barcode_str)
# 确保是纯数字字符串
if not barcode_str.isdigit():
# 只保留数字字符
barcode_str = re.sub(r'\D', '', barcode_str)
return barcode_str