May 2012
M T W T F S S
« Jul «-»  
 123456
78910111213
14151617181920
21222324252627
28293031  

Leaving Chris World?

Why not bring back a souvenir?

Archives

Round Integers


Escaping noise means I wind up in the library.
I just wish I was there because I felt like leaving
and not because I was feeling repelled by noise.

 
"""
Rounding Integers
 
There's always rounding for floats but I've
never seen anything for integers.
 
So I wrote this round_to function for integers
to show them some love.
In fact, a problem from work inspired me to figure
it out.
 
x is the number to be rounded
and place is the number of
places to round to.
 
"""
 
def round_to(x,place):
     return x - x%(10**place)
 
exit
 
Demonstration of rounding a strangely
orderly number...

>>> round_to(987654321,0)
987654321
>>> round_to(987654321,1)
987654320
>>> round_to(987654321,2)
987654300
>>> round_to(987654321,3)
987654000
>>> round_to(987654321,4)
987650000
>>> round_to(987654321,5)
987600000
>>> round_to(987654321,6)
987000000
>>> round_to(987654321,7)
980000000
>>> round_to(987654321,8)
900000000
>>> round_to(987654321,9)
"""

Notes

Simulated Flames

Pancakes made me sick.

2 comments to Round Integers

  • Thanks for the birthday song on Tuesday. I saved the message so I can listen to it again and again. Haha. ^_^ It was good to hear from you. I was surprised you remembered, but in a good way. Lurv, Vi

  • Hey, don’t insult pancakes! Those saved many breakfasts, you know. And baseball.

You must be logged in to post a comment.