How to round a floating point to nearest 10?

W

Will Rocisky

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

Peter Otten

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

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.

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

Peter
 
W

Will Rocisky

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

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.

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

Peter

thankssss
 
J

John Machin

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.
 
M

Mensanator

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).
1.23e-001

It all depends on what the OP actually wants. He's free to ignore
my example.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top