Answers for "how to return decimal value in python"

2

python decimal()

Decimal is a python library that allows for more precise decimals, without a 
large amount of floating point errors.
from decimal import *
getcontext().prec = n
The above line let''s you change the precision of the decimal.
i.e.
getcontext().prec = 6
print(Decimal(1) / Decimal(7))
Decimal('0.142857')
You can see that it has 6 digits of prescision.
There are useful functions, 
.log10
.sqrt
.exp
.ln
First, Decimal(x).log10 returns log10(x).
I.e.
print(Decimal(100).log10)
2
Then, Decimal(x).sqrt returns the square root of a number (duh).
Returns the square root of a number
getcontext().prec = 28
Decimal(2).sqrt()
Decimal('1.414213562373095048801688724')
Then .exp is the natural anti-lograthm (natural lograthm is loge(x), where is
e is Euler''s (say Oiler''s) constant, or 2.718...
Since exponentiation is the opposite of lograthms,
Decimal(x).exp = e**x
getcontext().prec = 3
Decimal(1).exp()
Decimal('2.718')
Finally, .ln is the natural lograthm, or loge(x) .
For more info check out 
https://docs.python.org/3/library/decimal.html
.
Posted by: Guest on July-30-2021
2

what should you call a decimal value in python

x = 13.949999999999999999
g = float("{:.2f}".format(x))
Posted by: Guest on May-20-2020

Code answers related to "how to return decimal value in python"

Python Answers by Framework

Browse Popular Code Answers by Language