1

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

Update 1 - based on comment/suggestion: Trying the formatting string. So a change to my question, do I need to bother with decimal at all, as long as I'm not adding numbers or maybe doing math on them?

print("float: {:10.14f}".format(price_float))
print("decimal: {:10.14f}".format(price_decimal))

Result:

float: 0.00000208229530
decimal: 0.00000208229530
3
  • 1
    Does this answer your question? How to format a floating number to fixed width in Python Commented Nov 12, 2021 at 0:17
  • 1
    "Decimal" means "base 10". It doesn't mean "not scientific notation". decimal.Decimal internally uses a base 10 representation, but that doesn't mean it won't print in scientific notation. Commented Nov 12, 2021 at 0:17
  • 2
    Writing price_float = 2.08229530000000007663 - never mind all the digits after that - is utterly and completely useless. Floats simply do not store numbers with that many significant digits of precision; turning this float into a Decimal later does not magically restore the lost precision. The only way to get the exact number into Decimal form is to pass it directly as a string. Commented Nov 12, 2021 at 0:26

2 Answers 2

3

I do that

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

Output

0.00000000002082295300
2.08229530000000007663E-11
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.