round does take negative ndigits parameter!
>>> round(46,-1)
50
may solve your case.
Answer from ch3ka on Stack Overflowround 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;
Videos
def round_down(num, divisor):
return num - (num%divisor)
In [2]: round_down(19,10)
Out[2]: 10
In [3]: round_down(19,5)
Out[3]: 15
In [4]: round_down(10,10)
Out[4]: 10
This probably isn't the most efficient solution, but
def round_down(m, n):
return m // n * n
is pretty simple.
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.
If you always round to the largest previous power of 10, use $$r=10^{\lfloor\log_{10}(n)\rfloor},$$ where $\lfloor x\rfloor$ is the floor function.
I assume you want the order of magnitude of a given integer, $x$.
Fortunately, $m:=\lfloor \log_{10}(x)\rfloor$ gives exactly that. ($\lfloor • \rfloor$ is the floor function)
You can then consider $10^m$ 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
So I have a list of floats that I am manipulating in a function. On the output of the function, I would like to round this to the nearest 100. So I would like to rounding to be like this: 157395.85 ----> 157400.00.
Here is an example of the code I wrote:
x = 157395.85
print(round(x, 4))
This did not run correctly. Another issue I noticed was that if the float had a zero at the last digit after the decimal, the zero was not listed. What I mean is that 50000.50 is stored as 50000.5.
Any help would be great!
Edit: To all that have commented, your solutions were amazing thank you so much! I've upvoted all your comments.