Print extremely large long in scientific notation in python

后端 未结 4 2088
长发绾君心
长发绾君心 2021-02-07 13:18

Is there a way to get python to print extremely large longs in scientific notation? I am talking about numbers on the order of 10^1000 or larger, at this size the standard print

4条回答
  •  别那么骄傲
    2021-02-07 13:27

    Try this:

    >>> def scientific_notation(v): # Note that v should be a string for eval()
            d = Decimal(eval(v))
            e = format(d, '.6e')
            a = e.split('e')
            b = a[0].replace('0','')
            return b + 'e' + a[1]
    
    >>> scientific_notation('10**1000')
    '1.e+1000'
    >>> scientific_notation('10**1000')
    '1.e+1000'
    >>> sc('108007135253151**1000') # Even handles large numbers
    '2.83439e+14033'
    

提交回复
热议问题