Display many, many, MANY decimal places in python [duplicate] - python

This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 7 years ago.
I am approximating phi to 50 decimal places and want to return the value I have computed. I do not want to print it otherwise I can't use it in calculations.
How can I do this because python only wants to display 11 decimal places or something like that?
Thanks

python floats do not have that precision. you need to use python decimal:
from decimal import getcontext, Decimal
getcontext().prec = 50
print(Decimal(1) / Decimal(7))
the drawback is that calculations with these will take much more time than the ones with float.

Related

how to sum to large string number in python [duplicate]

This question already has an answer here:
Is Python's Decimal class variable width?
(1 answer)
Closed 1 year ago.
How to sum these two large strings, if I transform to FLOAT it loses the accuracy.
str1= '5.123654879542658'
str2= '8.777548795426584'
str(float(string1) + float(string2))
The decimal package is for high precision (28 places by default) maths:
>>> from decimal import Decimal
>>> str1= '5.123654879542658'
>>> str2= '8.777548795426584'
>>> Decimal(str1) + Decimal(str2)
Decimal('13.901203674969242')
Update: #KarlKnechtel seems to have found a close duplicate, oh well...

Python: How do i Remove a CERTAIN amount of decimal in a float [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 2 years ago.
so i had this problem where i generated numbers from 0-1 with a step of 0.01 but the numbers have so many decimals like 0.010101010101.I only need it in the form 0.01 with two decimals.
How do i remove the rest of decimals ?
You can format the decimal like
your_value = 0.1010101010
desired_value = '%.2f' % your_value
Now desired_value will have the decimal upto 2 places.
Or you could use decimal.
Look at the documentation in the link:
https://docs.python.org/3/library/decimal.html
Round num to digits places. round(num, digits)

I want when a random number to show as 2 decimal places even if it comes up as an integer [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 3 years ago.
I have a random number generated, then I use round to get it to 2 decimal places.
Occasionally I get an integer such as "2" i would like this to be displayed as "2.00"
Or when I get "3.1" to be displayed as "3.10"
I have tried rounding to 2 d.p using round()
import random
#this creates a number between 2 and 5.99 by adding a decimal to an integer then rounds the sum to 2 d.p
def second_question():
temp_var_4 = random.randint(2,5) + round(random.random(),2)
print(temp_var_4)
second_question()
No error messages just returning some numbers to integers or to one d.p
You can control the output formatting explicitly by using a format string:
print('%.2f' % temp_var_4)

Index number of decimals in float - python [duplicate]

This question already has answers here:
Print pi to a number of decimal places
(8 answers)
Closed 4 years ago.
I'm writing a program that estimates the value of pi. I want to implement that you in the command line can specify when the result have the specified number of correct decimals.
Example:
python est_pi.py 3
should end the script when the estimation is 3.141...
Is it possible to have a variable real_pi = 3.14159... and then index into the number of decimals or is there any other solution to this problem?
If You can round your result using:
round(0.333333, N)
Where N is the parameter in input of your script
all the details are there: Round float to x decimals?
When you reached the needed precision, you can format the display through:
format(66.66666666666, '.'+str(N)+'f')
That will display your 66.666... with N digits.
In python 3.6 you have the f-string:
value = 2.34558
precision = N
width = 4
print(f'result: {value:{width}.{precision}f}')
Everything is detailed there: Limiting floats to two decimal points

Python Converts Float Incorrectly [duplicate]

This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Closed 6 years ago.
Can someone tell me what I'm missing here? This is using Python 2.7.11:
print float(148.95)
print float(148.95)*100
print int(float(148.95)*100)
Why does this print:
148.95
14895.0
14894 <--- Shouldn't this be 14895?
148.95 is not a number that can be exactly represented using floating point. The number internally stored is actually 148.94999999999998863131622783839702606201171875. When you multiply by a hundred, you get 14894.999999999998181010596454143524169921875. When you convert that to integer, it cuts off the .999... and you're left with 14894.
If you want a data type that can exactly represent numbers with at least two decimal places of precision, consider using Decimal.
>>> from decimal import Decimal
>>> x = Decimal("148.95")
>>> print x
148.95
>>> print x*100
14895.00
>>> print int(x*100)
14895

Categories

Resources