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
Related
This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to divide floats by each other but am having a hard time getting accurate results. I understand that computers store floats in a way where the value stored is not exact to the given number. I am simply looking for a way where I can get specific results when working with floats.
input:
x = 2.4
y = 0.2
print(x/y)
Output:
11.999999998
I highly recommend to use decimals
Example
from decimal import Decimal
x = Decimal("2.4")
y = Decimal("0.2")
print(x / y) # 12
Notice we passing number as string, as passing float numbers would have the same problem you pointed out.
But care with comparison, as 12 == x / y evaluates to False
This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 2 years ago.
This is my code so far:
number = 201234
print(f'number is {number:,.0f}')
This prints: number is 201,234
However I want it to print: number is 200,000
I've tried using print(f'number is {number:,.1g}') but this prints in scientific notation like so: number is 2e+05
Is there a simple way to format this to get the desired outcome?
Use the round() function with a negative argument.
number = 201234
print(f'number is {round(number, -5):,.0f}')
Prints
number is 200,000
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)
This question already has answers here:
Truncate to three decimals in Python
(21 answers)
Closed 3 years ago.
I would like to truncate a floating point number at the third decimal place. However, I don't want to round the number.
float("{0:.3f}".format(132.32579))
The code above produces 132.326 but I want it to produce 132.325. Any help is appreciated
You can round to 1 digit more, slice the string for ignoring the last digit and convert back to float:
print( float("{0:.4f}".format(132.32579) [:-1]))
You can avoid the string conversion/rounding by multiplying by 1000, converting to int and dividing by 1000.0 again:
print( float(int(132.32579*1000)/1000.0))
Output (both):
132.325
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.