This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 5 years ago.
Can we get up to 2 precision values when we add 2 float values of containing 0 as 2nd precision without changing its type as Float
I have a snippet like
a = 1.20+1.20
print a
the output should be like
2.40
but I got the output as 2.4 because python rounds off
I've tried like this ,
from decimal import *
getcontext().prec = 3
Decimal(1.20)+Decimal(1.20)
Decimal('2.40')
But every time I need to change the precision value!
can we get as 2.40 without changing its type?
It should contain a float value as 2.40, not a string!
Yes! you can round a float number.
>>> round(2.675, 2)
2.67
You can see more details in python's documentation
Related
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed last year.
I have two float values 1000.4 and 700.7
The result of 1000.4 - 700.7 returns me 299.69999999999993. I did my research,Decimal would be a good option to do the calculation here print(Decimal('1000.4') - Decimal('700.7')) and it returns 299.7
I have a question. what if I have a value 14.5 how can I print it as 14.50?
I tried getcontext().prec = 2 and it didn't help and would made the print(Decimal('1000.4') - Decimal('700.7')) returns 3.0E+2 which isn't what I want.
You can use f-strings to custom the leading zeros/limit the precision.
n=14.5
print(f"{n:.2f}")
Here, it would print only the first two decimals. (14.50).
This can be done with formatting for both floats and Decimal.
In [1]: print("{:.2f}".format(1.234))
1.23
In [2]: from decimal import Decimal
In [3]: print("{:.2f}".format(Decimal(1.234)))
1.23
This question already has answers here:
How to display a float with two decimal places?
(13 answers)
Closed 3 years ago.
I want number from input to print out with 2 decimal places. You can assume that the number will always be a float.
num = 20.0
Desired output - 20.00.
I've tried this code:
num = round(num, 2)
num = float('{0.2f}'.format(num))
print(num)
This should work
print('{0.2f}'.format(num))
When you turn this string back into a float with float() the formatting is lost.
No matter what you do to the float value, as long as it is still a float, it does not have any internal concept of decimal places.
If you want to display two decimal places, then that happens when you convert to text - which everything you print is, whether you asked for the conversion or not. You cannot make num "be" 20.00 as opposed to 20.0, because those aren't actually different things. (And keep in mind that the float simply cannot represent all decimal values exactly.)
Therefore, we use string formatting in the print call:
num = 20.0
print('{.2f}'.format(num))
# Or, using f-strings:
print(f'{num:.2f}')
This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 5 years ago.
I have the following:
'{0:n}'.format(0/10.0)
which evaluates to
0
I want it to evaluate to
0.0
That is, there should always be 1 decimal place. What is the correct format for this?
Thanks.
print('{0:3.1f}'.format(0/10.0)) # '0.0'
f for fixed-point notation; 3 for the total number of places to be reserved for the whole string, 1 for the decimal places.
if your number is too big to fit into the 3 reserved spaces, the string will use more space:
print('{0:3.1f}'.format(10.0)) # '10.0'; 4 places
with a more recent python version you can use f-strings (i put the value in a variable for more legibility)
x= 0/10.0
print('f{x:3.1f}')
In [24]: "{0:.1f}".format(0)
Out[24]: '0.0'
If you change the data to float you the decimal .
a= 1.0 # "a" is a float
a= 1 # "a" is now a integer
try that :)
This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Closed 5 years ago.
Looks like both 4.5 and 5.5 have exact float representations in Python 3.5:
>>> from decimal import Decimal
>>> Decimal(4.5)
Decimal('4.5')
>>> Decimal(5.5)
Decimal('5.5')
If this is the case, then why
>>> round(4.5)
4
>>> round(5.5)
6
?
In Python 3, exact half way numbers are rounded to the nearest even result. This behavior changed in Python 3
The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.round([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.
Python 3 uses Bankers Rounding, which rounds .5 values to the closest even number.
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here