Convert float number to string with engineering notation (with SI prefix) in Python

核能气质少年 提交于 2019-12-19 09:15:20

问题


I have a float number such as x=23392342.1

I would like to convert it to a string with engineering notation (with metric prefix)

http://en.wikipedia.org/wiki/Engineering_notation http://en.wikipedia.org/wiki/Metric_prefix

So in my example 23392342.1 = 23.3923421E6 = 23.3923421 M (mega)

I would like to display 23.3923421 M


回答1:


Here is a function inspired from Formatting a number with a metric prefix?

metric.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math


def to_si(d, sep=' '):
    """
    Convert number to string with SI prefix

    :Example:

    >>> to_si(2500.0)
    '2.5 k'

    >>> to_si(2.3E6)
    '2.3 M'

    >>> to_si(2.3E-6)
    '2.3 µ'

    >>> to_si(-2500.0)
    '-2.5 k'

    >>> to_si(0)
    '0'

    """

    incPrefixes = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
    decPrefixes = ['m', 'µ', 'n', 'p', 'f', 'a', 'z', 'y']

    if d == 0:
        return str(0)

    degree = int(math.floor(math.log10(math.fabs(d)) / 3))

    prefix = ''

    if degree != 0:
        ds = degree/math.fabs(degree)
        if ds == 1:
            if degree - 1 < len(incPrefixes):
                prefix = incPrefixes[degree - 1]
            else:
                prefix = incPrefixes[-1]
                degree = len(incPrefixes)

        elif ds == -1:
            if -degree - 1 < len(decPrefixes):
                prefix = decPrefixes[-degree - 1]
            else:
                prefix = decPrefixes[-1]
                degree = -len(decPrefixes)

        scaled = float(d * math.pow(1000, -degree))

        s = "{scaled}{sep}{prefix}".format(scaled=scaled,
                                           sep=sep,
                                           prefix=prefix)

    else:
        s = "{d}".format(d=d)

    return(s)


if __name__ == "__main__":
    import doctest
    doctest.testmod()

and its usage:

from metric import to_si
d = 23392342.1
print(to_si(d))

It will display

23.3923421 M



回答2:


A direct solution is to use the Decimal.to_eng_string method and then do a dictionary lookup to convert the exponent to appropriate metric prefix.




回答3:


Use the QuantiPhy package. It is a stable well documented and well supported package that is designed to do just what you want.

>>> from quantiphy import Quantity

>>> v = Quantity(23.3923421E6)                                               
>>> str(v)                                                                   
'23.392M'                                                                    

>>> v.render(prec='full')                                                    
'23.3923421M'

Generally people use SI unit prefixes with units, and Quantity is designed to combine the units with the number.

>>> v = Quantity(23.3923421E6, 'V')
>>> print(v)
23.392 MV

>>> f = Quantity('23.3923421 MHz')
>>> print('{}'.format(f))
23.392 MHz

Quantity subclasses float, so you can use quantities in expressions as you would a float:

>>> t = 1/f                                                                  
>>> print(t)                                                                 
4.274903281275114e-08

>>> t = Quantity(t, 's')                                                     
>>> print(t)
42.749 ns


来源:https://stackoverflow.com/questions/15733772/convert-float-number-to-string-with-engineering-notation-with-si-prefix-in-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!