This question already has answers here:
Round to nearest 0.05 using Python
(9 answers)
Closed 8 years ago.
I am trying to round up the right hand side (fractional part) of a float.
I want to round it up to 5
x = 0.43
expected outcome
0.45
I can convert the float into an int and then split the string by "." and then round the right hand side of the decimal point but I dont think this is the best method available.
Is there a function available for this type of task?
Thanks
This function will give you the desired output.
def round_to(n, precision=0.5):
correction = 0.5 if n >= 0 else -0.5
return int(n/precision+correction)*precision
If you call it:
>>> round_to(0.43)
0.45
I got this function from Python - Round to nearest 05
Related
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 2 years ago.
I wanna round after decimal point. I know that the function round does it but when the number has only zeros after decimal point I also wanna keep the amount of zeros that I want, becouse python leaves only 1 zero.
round(54.983839,2) == 54.98
round(54.0, 2) == 54.0 # and here I want 54.00
Use string formatting in python: "%.2f" % (54.0).
You can use format():
print(format(54.0, ".2f"))
The output will be:
54.00
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
This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 6 years ago.
I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67-->70 and 64-->70. Here is my code for rounding so far:
##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)
I would appreciate it if you could answer simple and explain how it works.
One way of rounding up would be to use integer division to go down to the precision you want and then multiplying back up. e.g.,:
Sumrounded = SumOfusrinput // (-10) * (-10)
This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
I have the following piece of code which I run with python 2.7.9:
import math
print 0.6/0.05
print math.floor(0.6/0.05)
print int(0.6/0.05)
The output is:
12.0
11.0
11
Why is it turning my 12.0 to an 11.0 when I use floor or int? round() will work but does not suit my use case.
I have this running with 0.55,0.60,0.65,0.70... and everything works fine except for 0.6.
Any idea?
If you know the required resolution in advance, you can multiply your input numbers accordingly and then make the required calculations safely.
Let's say that the resolution is 0.01. Then:
# original inputs
n = 0.6
d = 0.05
# intermediate values
n1 = int(n*100)
d1 = int(d*100)
# integer calculation
r = n1 // d1
# or
# floating point calculation
r = float(n1) / float(d1)