By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

How to round up/down integer number?

P: 20
hi,

i want my 2.25x10^12 to be rounded to 2x10^12 and 2.75x10^12 to 3x10^12.

How can i do that?

thanx
Sep 13 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 621
You probably have to roll your own function, i.e. convert to string, parse from right to left and split on the first non zero number, round as desired and append/multiply by the right-most number of zeros.
Sep 13 '10 #2

Expert 100+
P: 621
You probably have to roll your own function, i.e. convert to string, parse from right to left and split on the first non zero number, round as desired and append/multiply by the right-most number of zeros. Unless you always know how many zeros come after the digits.
Sep 13 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Following is a function I use in my work for rounding off to a predetermined increment:
Expand|Select|Wrap|Line Numbers
  1. >>> def round_length_near(length, increment=.0625):
  2. ...     if not increment:
  3. ...         return length
  4. ...     return round(length/float(increment)) * increment
  5. ... 
  6. >>> round_length_near(22500000000, 10000000000)
  7. 20000000000.0
  8. >>> round_length_near(27500000000, 10000000000)
  9. 30000000000.0
It requires converting the string to int or float.
Expand|Select|Wrap|Line Numbers
  1. >>> "%e" % 22500000000
  2. '2.250000e+010'
  3. >>> float('2.250000e+010')
  4. 22500000000.0
  5. >>> 
Sep 13 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.