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
Related
This question already has answers here:
How can I format a float with given precision and zero padding?
(2 answers)
Closed 9 months ago.
How can I get a float number with leading zeros and decimal precision?
For example:
number = 1.8315454
and I want to print "01.83"
print(f"{number:05.2f}")
the 0 indicates to fill with leading 0, the ยด5"teh total of characters in the output: digitis + decimal point, the "." indicates that decimal places should be printed, the "2" indicates that 2 decimal digits are wanted, the 'f' indicates it is a floating point number.
use string formating (f-string):
number = 1.8315454
print(f"{number:05.2f}")
output:
>> 01.83
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:
How to use Python string formatting to convert an integer representing cents to a float representing dollars?
(3 answers)
Limiting floats to two decimal points
(35 answers)
Closed 4 years ago.
I would like to truncate a number with only two positions after decimal point and return this number. But when I use the string format as float(" % .2f" % number), it returns 1.0 when the given number is like 1.0000. How could I resolve this problem? Thanks a lot.
It seems like my description was not clear enough. Let me take the 1.0005 as an example. I would like to return the number 1.00 with only two positions after the decimal point, rather than a string "1.00". So I choose to use string format to truncate the original number 1.0005 to a string "1.00" which satisfies the requirement of decimal points but it is still a string, I would like to return a number. So I use the float to convert it to a float number, but it seems like float() in gonna truncate the number to 1.0.
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
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