= 2: return num,._pycharm如何将数字转为亿">
赞
踩
- """
- @author: zhangjun.xue
- @time: 2020/5/20 13:14
- @file: unit_test.py
- @desc: python实现 数值 转换为 万,亿单位,保留3位小数
- """
-
-
- def str_of_num(num):
- '''
- 递归实现,精确为最大单位值 + 小数点后三位
- '''
- def strofsize(num, level):
- if level >= 2:
- return num, level
- elif num >= 10000:
- num /= 10000
- level += 1
- return strofsize(num, level)
- else:
- return num, level
- units = ['', '万', '亿']
- num, level = strofsize(num, 0)
- if level > len(units):
- level -= 1
- return '{}{}'.format(round(num, 3), units[level])
-
-
- if __name__ == "__main__":
- print(str_of_num(1))
- print(str_of_num(2))
- print(str_of_num(10))
- print(str_of_num(11))
- print(str_of_num(100))
- print(str_of_num(123))
- print(str_of_num(1000))
- print(str_of_num(1234))
- print(str_of_num(10000))
- print(str_of_num(11923))
- print(str_of_num(5463443))
- print(str_of_num(54634434))
- print(str_of_num(97736213623))
- print(str_of_num(977362136234))
- print(str_of_num(9773621362345))
- print(str_of_num(97736213623456))
STD OUT:
- 1
- 2
- 10
- 11
- 100
- 123
- 1000
- 1234
- 1.0万
- 1.192万
- 546.344万
- 5463.443万
- 977.362亿
- 9773.621亿
- 97736.214亿
- 977362.136亿
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。