How to round a floating point to nearest 10?

Discussion in 'Python' started by Will Rocisky, Aug 9, 2008.

  1. Will Rocisky

    Will Rocisky Guest

    I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    How can I achieve that?
     
    Will Rocisky, Aug 9, 2008
    #1
    1. Advertising

  2. Will Rocisky

    John Machin Guest

    On Aug 9, 9:31 pm, Will Rocisky <> wrote:
    > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > How can I achieve that?


    >>> import decimal
    >>> [decimal.Decimal(int(round(x, -1))) for x in (76.1, 74.9)]

    [Decimal("80"), Decimal("70")]
    >>>
     
    John Machin, Aug 9, 2008
    #2
    1. Advertising

  3. Will Rocisky

    Peter Otten Guest

    Will Rocisky wrote:

    > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > How can I achieve that?


    >>> help(round)


    Help on built-in function round in module __builtin__:

    round(...)
    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number. Precision may be negative.


    >>> for f in 74.9, 75.0, 75.1:

    .... print "%r --> %r" % (f, round(f, -1))
    ....
    74.900000000000006 --> 70.0
    75.0 --> 80.0
    75.099999999999994 --> 80.0

    Peter
     
    Peter Otten, Aug 9, 2008
    #3
  4. Will Rocisky

    Will Rocisky Guest

    On Aug 9, 5:46 pm, Peter Otten <> wrote:
    > Will Rocisky wrote:
    > > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > > How can I achieve that?
    > >>> help(round)

    >
    > Help on built-in function round in module __builtin__:
    >
    > round(...)
    >     round(number[, ndigits]) -> floating point number
    >
    >     Round a number to a given precision in decimal digits (default 0 digits).
    >     This always returns a floating point number.  Precision may be negative.
    >
    > >>> for f in 74.9, 75.0, 75.1:

    >
    > ...     print "%r --> %r" % (f, round(f, -1))
    > ...
    > 74.900000000000006 --> 70.0
    > 75.0 --> 80.0
    > 75.099999999999994 --> 80.0
    >
    > Peter


    thankssss
     
    Will Rocisky, Aug 9, 2008
    #4
  5. Will Rocisky

    Daniel Klein Guest

    On Sat, 9 Aug 2008 04:31:38 -0700 (PDT), Will Rocisky <>
    wrote:

    >I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    >How can I achieve that?


    >>> for n in (74.9, 76.1):

    print int((n+5)/10)*10

    70
    80

    Dan
     
    Daniel Klein, Aug 9, 2008
    #5
  6. Will Rocisky

    Mensanator Guest

    On Aug 9, 6:31�am, Will Rocisky <> wrote:
    > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > How can I achieve that?


    >>> print '%.0e' % 74.9

    7e+01
    >>> print '%.0e' % 76.1

    8e+01
     
    Mensanator, Aug 9, 2008
    #6
  7. Will Rocisky

    John Machin Guest

    On Aug 10, 1:19 am, Mensanator <> wrote:
    > On Aug 9, 6:31 am, Will Rocisky <> wrote:
    >
    > > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > > How can I achieve that?
    > >>> print '%.0e' % 74.9

    > 7e+01
    > >>> print '%.0e' % 76.1

    >
    > 8e+01


    But:
    >>> print '%.0e' % 176.1

    2e+002

    Giving the Subject ("How to round a floating point to nearest 10?"),
    there's a strong presumption that the OP would want the answer to be
    180, not 200.
     
    John Machin, Aug 9, 2008
    #7
  8. Will Rocisky

    Mensanator Guest

    On Aug 9, 4:54 pm, John Machin <> wrote:
    > On Aug 10, 1:19 am, Mensanator <> wrote:
    >
    > > On Aug 9, 6:31 am, Will Rocisky <> wrote:

    >
    > > > I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
    > > > How can I achieve that?
    > > >>> print '%.0e' % 74.9

    > > 7e+01
    > > >>> print '%.0e' % 76.1

    >
    > > 8e+01

    >
    > But:>>> print '%.0e' % 176.1
    >
    > 2e+002


    Which would be correct if your goal was to restrain to
    1 significant digit.

    >
    > Giving the Subject ("How to round a floating point to nearest 10?"),
    > there's a strong presumption that the OP would want the answer to be
    > 180, not 200.


    Well, I can't read the OP's mind and the cases I HAVE encountered
    are concerned about the number of significant digits. When
    laboratories
    report 3 digits, all my manipulations (ppm conversion, dividing
    non-detect reporting limits by 2, comparison to TACO, etc. are
    required to also have exactly 3 digits of significance).

    >>> print '%.2e' % 0.00000123456

    1.23e-006
    >>> print '%.2e' % 123456

    1.23e+005
    >>> print '%.2e' % 0.123000456

    1.23e-001

    It all depends on what the OP actually wants. He's free to ignore
    my example.
     
    Mensanator, Aug 9, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    floating point round in C++

    John Smith, Dec 14, 2003, in forum: C++
    Replies:
    2
    Views:
    40,754
    Juergen Heinzl
    Dec 14, 2003
  2. ma740988
    Replies:
    13
    Views:
    1,065
    James Kanze
    Dec 14, 2008
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,350
    Saraswati lakki
    Jan 6, 2012
  4. Dermot Moynihan
    Replies:
    9
    Views:
    349
    Dermot Moynihan
    Jan 6, 2007
  5. Lucky Nl
    Replies:
    2
    Views:
    115
    Benoit Daloze
    May 26, 2010
Loading...

Share This Page