How to round up/down integer number?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peachdot
    New Member
    • Aug 2010
    • 20

    How to round up/down integer number?

    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
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      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.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Following is a function I use in my work for rounding off to a predetermined increment:
        Code:
        >>> def round_length_near(length, increment=.0625):
        ...     if not increment:
        ...         return length
        ...     return round(length/float(increment)) * increment
        ... 
        >>> round_length_near(22500000000, 10000000000)
        20000000000.0
        >>> round_length_near(27500000000, 10000000000)
        30000000000.0
        It requires converting the string to int or float.
        Code:
        >>> "%e" % 22500000000
        '2.250000e+010'
        >>> float('2.250000e+010')
        22500000000.0
        >>>

        Comment

        Working...