在基数之间转换数字。
baseconvert的Python项目详细描述
将任意有理数从任意(正整数)基数转换为任意 (正整数)基数。以元组或字符串形式输出数字。
- 任何有理数
- 任意精度
- 分数
- 循环/重复小数位数。
- 以元组、字符串或数字的形式输入数字。
- 以元组或字符串形式输出数字。
麻省理工学院许可证(麻省理工学院)版权所有(c)2016 Joshua Deakin
需要
- Python3
安装/卸载
安装:
pip install baseconvert
卸载:
pip uninstall baseconvert
快速启动
# base(number, input_base, output_base) >>> base((15, 15, 0, ".", 8), 16, 10) (4, 0, 8, 0, '.', 5) >>> base("FF0.8", 16, 10, string=True) '4080.5' >>> base("4080.5", 10, 16, string=True) 'FF0.8'
或从命令行
$ echo 4080.5 | python -m baseconvert -i 10 -o 16 FF0.8 $ python -m baseconvert -n 4080.5 -i 10 -o 16 FF0.8
元组表示法
数字表示为一个数字序列。每个数字都是以10为基数 整数值。将整数和 小数部分,由字符串句点表示。
(int, int, int, ... , '.', ... , int, int, int) ( integer part , '.', fractional part )
字符串表示
字符串数字(Z之后的值是升序Unicode):
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz | Value | Representation | |---------|----------------| | 0 - 9 | 0 - 9 | | 10 - 53 | A - Z | | 36 - 61 | a - z | | 62 + | unicode 123 + | For bases higher than 61 it's recommended to use tuple representation.
示例
# base(number, input_base, output_base) >>> n = (15,15,".",0,8) >>> base(n, 16, 10) (2, 5, 5, '.', 0, 3, 1, 2, 5) >>> base(n, 16, 10, string=True) '255.03125' >>> base("FF.08", 16, 10) == base((15,15,".",0,8), 16, 10) True # A callable BaseConverter object can also be created. # This is useful for when several numbers need to be converted. >>> b = BaseConverter(input_base=16, output_base=8) >>> b("FF") (3, 7, 7) >>> b((15, 15)) (3, 7, 7) >>> b("FF") == b((15,15)) True >>> base(0.1, 3, 10, string=True) '0.[3]'
循环位数
小数部分末尾的循环数字将用“[”括起来 以及字符串和元组表示中的“]”。这种行为可以是 通过设置base或baseconverter的循环参数关闭 对象为false。
>>> base("0.1", 3, 10, string=True) '0.[3]' >>> base("0.1", 3, 10, string=True, recurring=False) '0.3333333333'
最大分数深度
整数部分总是具有任意大小。分数深度(个 必须通过设置base的max_depth参数来指定 或BaseConverter对象(默认为10)。
>>> base("0.2", 10, 8) (0, '.', 1, 4, 6, 3, 1, 4, 6, 3, 1, 4) >>> base("0.2", 10, 8, max_depth=1) (0, '.', 1)