Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python library to convert between SI unit prefixes

Tags:

python

I'm looking for a python library which comes with support to convert numbers between various SI prefixes, for example, kilo to pico, nano to giga and so on.What would you recommend?

like image 557
Zed Avatar asked Jun 10 '12 15:06

Zed


1 Answers

I ported a simple function (original C version written by Jukka “Yucca” Korpela) to Python for formatting numbers according to SI standards. I use it often, for example, to set tick labels on plots, etc.

You can install it with:

pip install si-prefix

The source is available on GitHub.

Example usage:

from si_prefix import si_format

print si_format(.5)
# 500.0m  (default precision is 1)

print si_format(.01331, precision=2)
# 13.31m

print si_format(1331, precision=2)
# 1.33k

print si_format(1331, precision=0)
# 1k
like image 156
naitsirhc Avatar answered Sep 18 '22 03:09

naitsirhc