format_value.py 1018 B

12345678910111213141516171819202122232425262728293031
  1. def format_value(index, value):
  2. """格式化数据"""
  3. if value == '':
  4. return ''
  5. if value is not None:
  6. value = float(value)
  7. divisor = index['divisor']
  8. offset = index['offset']
  9. low_limit = index['low_limit']
  10. up_limit = index['up_limit']
  11. if divisor:
  12. value /= divisor
  13. if offset:
  14. value -= offset
  15. # 保留两位小数
  16. value = round(value, 3)
  17. if low_limit is not None and up_limit is not None: # 上下限都存在
  18. if low_limit <= value <= up_limit:
  19. return value
  20. elif low_limit is not None and up_limit is None: # 下限存在
  21. if low_limit <= value:
  22. return value
  23. elif up_limit is not None and low_limit is None: # 上限存在
  24. if value <= up_limit:
  25. return value
  26. else: # 都不存在
  27. return value
  28. # 不在范围舍弃数据
  29. return ''
  30. else:
  31. return ''