decimal in python

Solutions on MaxInterview for decimal in python by the best coders in the world

showing results for - "decimal in python"
Eleonora
22 Jan 2021
1Decimal is a python library that allows for more precise decimals, without a 
2large amount of floating point errors.
3from decimal import *
4getcontext().prec = n
5The above line let''s you change the precision of the decimal.
6i.e.
7getcontext().prec = 6
8print(Decimal(1) / Decimal(7))
9Decimal('0.142857')
10You can see that it has 6 digits of prescision.
11There are useful functions, 
12.log10
13.sqrt
14.exp
15.ln
16First, Decimal(x).log10 returns log10(x).
17I.e.
18print(Decimal(100).log10)
192
20Then, Decimal(x).sqrt returns the square root of a number (duh).
21Returns the square root of a number
22getcontext().prec = 28
23Decimal(2).sqrt()
24Decimal('1.414213562373095048801688724')
25Then .exp is the natural anti-lograthm (natural lograthm is loge(x), where is
26e is Euler''s (say Oiler''s) constant, or 2.718...
27Since exponentiation is the opposite of lograthms,
28Decimal(x).exp = e**x
29getcontext().prec = 3
30Decimal(1).exp()
31Decimal('2.718')
32Finally, .ln is the natural lograthm, or loge(x) .
33For more info check out 
34https://docs.python.org/3/library/decimal.html
35.
Anton
25 Jan 2020
1x = 13.949999999999999999
2g = float("{:.2f}".format(x))
Marco
29 Aug 2016
1>>> from decimal import *
2>>> getcontext().prec = 6
3>>> Decimal(1) / Decimal(7)
4Decimal('0.142857')
5>>> getcontext().prec = 28
6>>> Decimal(1) / Decimal(7)
7Decimal('0.1428571428571428571428571429')
8
queries leading to this page
module python decimaldecimal module in pythondecimal module python 3python 2 places decimal 3bpython decimal typepython decimal importincrease float precision pythonpython import decimalimport decimal pythonpython 3 decimal decimaldecimal in python 5cdefine float decimals pythonimport decimal in pythondecimal decimal pythonhow to have all decimals in pythonpython precisionpython get 3 digit floatbest way to set 3 decimal places on a float pythonfix float arithmetic pythonhow to specify float precision in pythonhow to print precision in pythonprecision of a float in pythonpython decimal modulefloat with precision pythonpython float decimal placesdecimal pythondecimal import pythonset persision in pythondecimal values in pythonpython decimal and floathow to make a decimal number in pythondecimal point conversion in pythonimport decimal module pythonpython fix float precisiondecimals pythonpython decimaklstandardize decimals floathow to use decimal module in pythondecimal in pythonhow to use decimal in pythonfix precision of float in pythonhow to make a big number into a decimal in pythonpython do not round float 10 deicmalspython decimal placesdecimal value in pythonpython float 0 decimalspython decimal c3 a7how to determined the decimal place in pythonpython floating point precisionpython returning zero for long decimal mathdecimal point pythonget value with no decimal palaces pythonpython decimal objectpython decimal number typespython precision pointset precision in pythonhow to take the values after the decimal point in pythonexact decimal math pythonadd a precision of 0 in pythonprecision float pythonhow to set the scale of a float in pythnopython number of digits after decimalhow to have only 3 decimal value in pythondecimal fucniton pythonpython float precisionset float point in pythonhow to get decimal numbers in pythonvariable decimal pythondecimal py pythoncontrolling no of decimals in pythonpoint precision in python 3decimal 28 27 27 29 pythonpython define precision of floatdecimal package pythonfloat set precisiondecimal up to 10 digits in pythonhow to stop getting decimal values when used 2f operator in pythonpython force float precisiondecimal type in pythonwhat should you call a decimal value in pythondecimal decimal python 3set float precision pythonpython decimalpython decimal varialvepython decimal 28 29decimal library pythonconvert numbers to 5 decimal places in pythonhow to change floating point pythonpython fixed precisionpython decimal precisionpython make decimalprecision in python floatdecimal in python