猿问

如何使用 SI 前缀(micro、milli、Mega、Giga 等)格式化数字?

我的数字范围从非常小到非常大,我想使用带有大小和后缀的“工程符号”来格式化它们:


n.nnn S

其中 1.0 <= n.nnn < 1000.,S 是度量 (SI) 前缀。所以:


1234.5e+13 => 12.35P

12345678 => 12.35M

1234 => 1.234K

1.234 => 1.234

0.1234 => 123.4m

1234.5e-16 => 1.235f

等我该怎么做,例如使用 Python?


慕沐林林
浏览 327回答 2
2回答

尚方宝剑之说

(以问答方式发布在这里,因为我一直在重新发明这段代码,其他人可能会发现它很有帮助。如果您看到改进,请随时调整它......)这是一种实现,可让您选择长后缀(例如“peta”)或短后缀(例如“P”),还可以让您选择显示的总位数(即精度):def si_classifier(val):&nbsp; &nbsp; suffixes = {&nbsp; &nbsp; &nbsp; &nbsp; 24:{'long_suffix':'yotta', 'short_suffix':'Y', 'scalar':10**24},&nbsp; &nbsp; &nbsp; &nbsp; 21:{'long_suffix':'zetta', 'short_suffix':'Z', 'scalar':10**21},&nbsp; &nbsp; &nbsp; &nbsp; 18:{'long_suffix':'exa', 'short_suffix':'E', 'scalar':10**18},&nbsp; &nbsp; &nbsp; &nbsp; 15:{'long_suffix':'peta', 'short_suffix':'P', 'scalar':10**15},&nbsp; &nbsp; &nbsp; &nbsp; 12:{'long_suffix':'tera', 'short_suffix':'T', 'scalar':10**12},&nbsp; &nbsp; &nbsp; &nbsp; 9:{'long_suffix':'giga', 'short_suffix':'G', 'scalar':10**9},&nbsp; &nbsp; &nbsp; &nbsp; 6:{'long_suffix':'mega', 'short_suffix':'M', 'scalar':10**6},&nbsp; &nbsp; &nbsp; &nbsp; 3:{'long_suffix':'kilo', 'short_suffix':'k', 'scalar':10**3},&nbsp; &nbsp; &nbsp; &nbsp; 0:{'long_suffix':'', 'short_suffix':'', 'scalar':10**0},&nbsp; &nbsp; &nbsp; &nbsp; -3:{'long_suffix':'milli', 'short_suffix':'m', 'scalar':10**-3},&nbsp; &nbsp; &nbsp; &nbsp; -6:{'long_suffix':'micro', 'short_suffix':'µ', 'scalar':10**-6},&nbsp; &nbsp; &nbsp; &nbsp; -9:{'long_suffix':'nano', 'short_suffix':'n', 'scalar':10**-9},&nbsp; &nbsp; &nbsp; &nbsp; -12:{'long_suffix':'pico', 'short_suffix':'p', 'scalar':10**-12},&nbsp; &nbsp; &nbsp; &nbsp; -15:{'long_suffix':'femto', 'short_suffix':'f', 'scalar':10**-15},&nbsp; &nbsp; &nbsp; &nbsp; -18:{'long_suffix':'atto', 'short_suffix':'a', 'scalar':10**-18},&nbsp; &nbsp; &nbsp; &nbsp; -21:{'long_suffix':'zepto', 'short_suffix':'z', 'scalar':10**-21},&nbsp; &nbsp; &nbsp; &nbsp; -24:{'long_suffix':'yocto', 'short_suffix':'y', 'scalar':10**-24}&nbsp; &nbsp; }&nbsp; &nbsp; exponent = int(math.floor(math.log10(abs(val))/3.0)*3)&nbsp; &nbsp; return suffixes.get(exponent, None)def si_formatter(value):&nbsp; &nbsp; '''&nbsp; &nbsp; Return a triple of scaled value, short suffix, long suffix, or None if&nbsp; &nbsp; the value cannot be classified.&nbsp; &nbsp; '''&nbsp; &nbsp; classifier = si_classifier(value)&nbsp; &nbsp; if classifier == None:&nbsp; &nbsp; &nbsp; &nbsp; # Don't know how to classify this value&nbsp; &nbsp; &nbsp; &nbsp; return None&nbsp; &nbsp; scaled = value / classifier['scalar']&nbsp; &nbsp; return (scaled, classifier['short_suffix'], classifier['long_suffix'])def si_format(value, precision=4, long_form=False, separator=''):&nbsp; &nbsp; '''&nbsp; &nbsp; "SI prefix" formatted string: return a string with the given precision&nbsp; &nbsp; and an appropriate order-of-3-magnitudes suffix, e.g.:&nbsp; &nbsp; &nbsp; &nbsp; si_format(1001.0) => '1.00K'&nbsp; &nbsp; &nbsp; &nbsp; si_format(0.00000000123, long_form=True, separator=' ') => '1.230 nano'&nbsp; &nbsp; '''&nbsp; &nbsp; scaled, short_suffix, long_suffix = si_formatter(value)&nbsp; &nbsp; if scaled == None:&nbsp; &nbsp; &nbsp; &nbsp; # Don't know how to format this value&nbsp; &nbsp; &nbsp; &nbsp; return value&nbsp; &nbsp; suffix = long_suffix if long_form else short_suffix&nbsp; &nbsp; if abs(scaled) < 10:&nbsp; &nbsp; &nbsp; &nbsp; precision = precision - 1&nbsp; &nbsp; elif abs(scaled) < 100:&nbsp; &nbsp; &nbsp; &nbsp; precision = precision - 2&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; precision = precision - 3&nbsp; &nbsp; return '{scaled:.{precision}f}{separator}{suffix}'.format(&nbsp; &nbsp; &nbsp; &nbsp; scaled=scaled, precision=precision, separator=separator, suffix=suffix)

Cats萌萌

您可以使用具有浮点类型和附加格式选项的Prefixed。>>> from prefixed import Float>>> f'{Float(1234.5e+13):.2h}''12.35P'>>> f'{Float(12345678):.2h}''12.35M'>>> f'{Float(1234):.2h}''1.23k'>>> f'{Float(1.234):.2h}''1.23'>>> f'{Float(0.1234):.2h}''123.40m'>>> f'{Float(1234.5e-16):.2h}''123.45f'
随时随地看视频慕课网APP

相关分类

Python
我要回答