Python Decimal - engineering notation for mili (10e-3) and micro (10e-6)

前端 未结 5 1352
情话喂你
情话喂你 2021-01-01 23:53

Here is the example which is bothering me:

>>> x = decimal.Decimal(\'0.0001\')
>>> print x.normalize()
>>> print x.normalize().to_         


        
5条回答
  •  太阳男子
    2021-01-02 00:13

    I realize that this is an old thread, but it does come near the top of a search for python engineering notation and it seems prudent to have this information located here.

    I am an engineer who likes the "engineering 101" engineering units. I don't even like designations such as 0.1uF, I want that to read 100nF. I played with the Decimal class and didn't really like its behavior over the range of possible values, so I rolled a package called engineering_notation that is pip-installable.

    pip install engineering_notation
    

    From within Python:

    >>> from engineering_notation import EngNumber
    >>> EngNumber('1000000')
    1M
    >>> EngNumber(1000000)
    1M
    >>> EngNumber(1000000.0)
    1M
    >>> EngNumber('0.1u')
    100n
    >>> EngNumber('1000m')
    1
    

    This package also supports comparisons and other simple numerical operations.

    https://github.com/slightlynybbled/engineering_notation

提交回复
热议问题