= 2: return num,._pycharm如何将数字转为亿">
当前位置:   article > 正文

python实现 数值 转换为 万,亿单位,保留3位小数(例:18000转换为1.8万)_pycharm如何将数字转为亿万单位

pycharm如何将数字转为亿万单位
  1. """
  2. @author: zhangjun.xue
  3. @time: 2020/5/20 13:14
  4. @file: unit_test.py
  5. @desc: python实现 数值 转换为 万,亿单位,保留3位小数
  6. """
  7. def str_of_num(num):
  8. '''
  9. 递归实现,精确为最大单位值 + 小数点后三位
  10. '''
  11. def strofsize(num, level):
  12. if level >= 2:
  13. return num, level
  14. elif num >= 10000:
  15. num /= 10000
  16. level += 1
  17. return strofsize(num, level)
  18. else:
  19. return num, level
  20. units = ['', '万', '亿']
  21. num, level = strofsize(num, 0)
  22. if level > len(units):
  23. level -= 1
  24. return '{}{}'.format(round(num, 3), units[level])
  25. if __name__ == "__main__":
  26. print(str_of_num(1))
  27. print(str_of_num(2))
  28. print(str_of_num(10))
  29. print(str_of_num(11))
  30. print(str_of_num(100))
  31. print(str_of_num(123))
  32. print(str_of_num(1000))
  33. print(str_of_num(1234))
  34. print(str_of_num(10000))
  35. print(str_of_num(11923))
  36. print(str_of_num(5463443))
  37. print(str_of_num(54634434))
  38. print(str_of_num(97736213623))
  39. print(str_of_num(977362136234))
  40. print(str_of_num(9773621362345))
  41. print(str_of_num(97736213623456))

STD OUT:

  1. 1
  2. 2
  3. 10
  4. 11
  5. 100
  6. 123
  7. 1000
  8. 1234
  9. 1.0
  10. 1.192
  11. 546.344
  12. 5463.443
  13. 977.362亿
  14. 9773.621亿
  15. 97736.214亿
  16. 977362.136亿

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/536620
推荐阅读
相关标签
  

闽ICP备14008679号