This question already has an answer here:
Python int float rounding
(1 answer)
Closed 3 years ago.
Can someone explain this behavior?
print("%.2f" % (model.x[1].value))
X = int(model.x[1].value)
print("%.2f" % X)
Output:
3.00
2.00
Thanks
When model.x[1].value is less than 3 but >= 2.995 then when printed to 2 decimal places it will round up to 3.00. But when you apply the int function it will be truncated to an integer of value 2, which will print as 2.00 when used with the .2f format.
>>> x = 2.994
>>> print(f"{x:.2f}")
2.99
>> x = 2.995
>>> print(f"{x:.2f}")
3.00
Thanks guys. The value is indeed not an integer. I was fooled by a mixed integer nonlinear programming solver. It seems that it doesn't round the solution to integer by itself.
Related
This question already has answers here:
How can I format a decimal to always show 2 decimal places?
(13 answers)
Closed 3 months ago.
i want to round off a float to 3 dp in python with 00 in the end if the float don't have 3 dp
like 15.4 into 15.400
thank you.
programme:
x=round(15.4)
result:
15.400
The "rounding" you are talking about can only be done if you convert the float to a string. This is usually only done for display purposes. In this case you can use a so-called f-string to do this formatting:
x = 15.4
print(f"{x:.3f}")
Hello its pretty simple you can do something like this
a=15.4
b=("%.3f" % a)
print(b)
15.4 and 15.400 are the same number. round() returns a number. What you want is to have a different representation when you convert it to a string.
You need to do string formatting. Just copying the other answers here, there are two ways.
f-strings:
n = 15.4
n_str = f"{n:.3f}"
%-formatting:
n_str = "%.3f" % n
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 4 years ago.
I want to format my float number with 2 digit after decimal.
>>> x =5.0
>>> y=float("{:0.2f}".format(x))
>>> y
5.0
i want my output in this format:
5.00
For newer version of python you can use:
x = 5.0
print(f' x: {x:.2f}')
out put will be:
x: 5.00
for more about this style see: f-string
You can do it by
In [11]: x = 5
In [12]: print("%.2f" % x)
5.00
In [13]:
Your answer was correct. you just misplaced the colon:
print "{:.2f}".format(5.0)
#output:
'5.00'
;)
This question already has answers here:
How do you check whether a number is divisible by another number?
(9 answers)
Closed 6 years ago.
Wondering what is the most efficient way to check if an integer can or cannot be divided by another number (could be float) in Python 2.7. Or more general, what is the most efficient way to check if an integer can be divided by n (n could be a float number) in Python 2.7.
My pain point is, if I try to get x/n, it is always an integer.
Try
if x % n == 0 :
hope this helps !
Here:
x = 25
y = 2.5 # Or something
if not x % y: # Works with float too
print 'can divide'
else:
print 'cannot divide'
This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
I have a quick question in terms of decimals points in Python 2.
Here is the code in Python 2:
a = 1500/1589
print (round(a,2))
#0.0
I thought it would print out a = 0.94 as it did in Python 3. I am just wondering how I could get 2 decimals for my answer.
You need to specify that at least one of the inputs to the division is a floating point number by adding an explicit decimal point.
a = 1500. / 1589
print round(a, 2)
# 0.94
If you do not do this, both 1500 and 1589 are treated as integers and the result of 1500/1589 is required to be an integer (0) as well
1500 / 1589
# 0
print round(1500 / 1589, 2)
# 0.0
As a side note, if you want to print a number with two decimal places, a far easier method is to create a formatted string:
print '%0.2f' % (1500. / 1589)
# 0.94
print '{:.2}'.format(1500. / 1589)
# 0.94
That's because you're making an integer division. For instance do:
a = 1500.0/1589.0
print round(a,2)
Or if you have integers as input:
a = float(1500)/float(1589)
print round(a,2)
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 8 years ago.
While solving Project Euler problems with Python (which I am a beginner at), I got this following error. The question is to find the sum of the digits of 2^1000. For that I wrote the following code :
sum=0
x=2**1000
while(x):
sum += x%10
print(sum) #Just to check whats happening
x /= 10
print("\n"*5)
print("Sum = ",sum)
For this, I get decimal added up somewhere in between.
Output :
6
10.0
10.0
12.0
16.0
....
1116.0
1122.0
1131.625 #Why does the decimal get added?
1138.59375
.....
1181.495136589947
1186.5812084526442
1188.089815638914
1195.240676357541
1195.9557624294036
1197.0272710365898
1197.1344218973084
1197.1451369833803
1197.1462084919874
.....
1197.1463275484991 #This number gets repeated a lot of times
1197.1463275484991
1197.1463275484991
Sum = 1197.1463275484991
Please explain what's going on and help.
Use integer division instead of floating point:
x //= 10
Do not know if you're looking for an alternative implementation, but this might be more straightforward if you don't want to risk crossing over into floating point land.
# Python 2.7
x = str(2**1000)
print sum([int(i) for i in x])