format_value.py 982 B

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