简体   繁体   中英

Python - show float as a decimal in a print statement

I'm looking at prices of low-capitalization crypto meme coins. I want to format and show as a decimal in the print statement, up about 10 digits. For example, the price of Saitama as shown on CoinGekco is $0.000000100861.

I don't understand if I'm using the Decimal library wrong, or if this is just a print/formatting issue.

from decimal import Decimal
# I think everything after the 7663 is irrelevant, this is a number I'm getting back 
# from a Uniswap API.  It could be the price in ETH, that is my next issue.
price_float = 2.08229530000000007663121204885725199461299350645049344166181981563568115234375E-11
price_decimal = Decimal(str(price_float))
print("float:", price_float) 
print("decimal:", price_decimal)

Results:

float: 2.0822953e-11
decimal: 2.0822953E-11

Desired Results:

float: 2.0822953e-11
decimal: .000000000020822953 

But if I try an exponent less than 6 it seems to work:

price_float = 2.08229530000000007663121204885725199461299350645049344166181981563568115234375E-6

Result:

decimal: 0.0000020822953000000003

You are probably looking for something like print("{:12.10f}".format(price_decimal)) . The output format is not controlled here by the decimal library

I do that

print(f'{price_float:.20f}')
print(f'{price_float:.20E}')

Output

0.00000000002082295300
2.08229530000000007663E-11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM