Here's how to round a number to the nearest tenth:
>>> round(21.3331, 1)
21.3
Here's how to print out a floating point number with one decimal point:
>>> print "%.1f" % (21.3331,)
21.3
Note that if you do it using %d it will get printed as rounded to the nearest integer:
>>> print "%d" % (21.3331,)
21
See the String Formatting Operations docs for more on how the % formatting works.
Here's how to round a number to the nearest tenth:
>>> round(21.3331, 1)
21.3
Here's how to print out a floating point number with one decimal point:
>>> print "%.1f" % (21.3331,)
21.3
Note that if you do it using %d it will get printed as rounded to the nearest integer:
>>> print "%d" % (21.3331,)
21
See the String Formatting Operations docs for more on how the % formatting works.
Here's as simple one.
roundf(1.345 * 100) / 100
You simply multiply it by 100 before dividing it by 100 which preserves the value to 2 decimal places. This should be much more efficient than transforming it to a string and back.
Videos
What is the Python round function?
How to use round function in Python with decimal places?
How to round numbers in a list using Python?
round does take negative ndigits parameter!
>>> round(46,-1)
50
may solve your case.
You can use math.ceil() to round up, and then multiply by 10
Python 2
import math
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
Python 3 (the only difference is you no longer need to cast the result as an int)
import math
def roundup(x):
return math.ceil(x / 10.0) * 10
To use just do
>>roundup(45)
50
Hi, I'm wondering how I'd go about rounding any given integer to the nearest ten. (i.e, 11 into 10, 156 into 160, etc.)
EDIT: Got it, for anybody wondering the code I used was: x = floor(x)/10; x = x*10;
If you always round to the largest previous power of 10, use
where
is the floor function.
I assume you want the order of magnitude of a given integer, .
Fortunately, gives exactly that. ($\lfloor โข \rfloor$ is the floor function)
You can then consider for the nearest (lower) power of 10.
Actually, you could still use the round function:
>>> print round(1123.456789, -1)
1120.0
This would round to the closest multiple of 10. To 100 would be -2 as the second argument and so forth.
round() can take ints and negative numbers for places, which round to the left of the decimal. The return value is still a float, but a simple cast fixes that:
>>> int(round(5678,-1))
5680
>>> int(round(5678,-2))
5700
>>> int(round(5678,-3))
6000
TL;DR:
round(x)
will round it and change it to integer.
You are not assigning round(h) to any variable. When you call round(h), it returns the integer number but does nothing else; you have to change that line for:
h = round(h)
to assign the new value to h.
As @plowman said in the comments, Python's round() doesn't work as one would normally expect, and that's because the way the number is stored as a variable is usually not the way you see it on screen. There are lots of answers that explain this behavior.
One way to avoid this problem is to use the Decimal as stated by this answer.
In order for this answer to work properly without using extra libraries it would be convenient to use a custom rounding function. I came up with the following solution, that as far as I tested avoided all the storing issues. It is based on using the string representation, obtained with repr() (NOT str()!). It looks hacky but it was the only way I found to solve all the cases. It works with both Python2 and Python3.
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
return float(num[:-1])
Tests:
>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>>
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0
Finally, the corrected answer would be:
# Having proper_round defined as previously stated
h = int(proper_round(h))
Tests:
>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1 # should be 7
The gotcha here is that the dec-th decimal can be 9 and if the dec+1-th digit >=5 the 9 will become a 0 and a 1 should be carried to the dec-1-th digit.
If we take this into consideration, we get:
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
a = num[:-2-(not dec)] # integer part
b = int(num[-2-(not dec)])+1 # decimal part
return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
return float(num[:-1])
In the situation described above b = 10 and the previous version would just concatenate a and b which would result in a concatenation of 10 where the trailing 0 would disappear. This version transforms b to the right decimal place based on dec, as a proper carry.
Use round(x, y). It will round up your number up to your desired decimal place.
For example:
>>> round(32.268907563, 3)
32.269