round down to nearest number

N

noydb

How do you round down ALWAYS to nearest 100? Like, if I have number
3268, I want that rounded down to 3200. I'm doing my rounding likeBut, how to round DOWN?
 
I

Ian Kelly

How do you round down ALWAYS to nearest 100?  Like, if I have number
3268, I want that rounded down to 3200.  I'm doing my rounding like
3200

For more complicated cases, Decimal objects allow you to specify
alternate rounding modes.
 
N

noydb

hmmm, okay.

So how would you round UP always? Say the number is 3219, so you want
3300 returned.
 
N

noydb

That {>>> (3219 + 99) // 100} doesnt work if the number is other then
4 digits.


(for rounding up to nearest 100):4
 
M

MRAB

That {>>> (3219 + 99) // 100} doesnt work if the number is other then
4 digits.


(for rounding up to nearest 100):
400

Those are all rounded up to the nearest 100 correctly.
 
I

Ian Kelly

400

Those are all rounded up to the nearest 100 correctly.

One thing to be aware of though is that while the "round down" formula
works interchangeably for ints and floats, the "round up" formula does
not.
3300.0

A more consistent alternative is to negate the number, round down, and
then negate again.
3400.0

Cheers,
Ian
 
A

Arnaud Delobelle

One thing to be aware of though is that while the "round down" formula
works interchangeably for ints and floats, the "round up" formula does
not.

3300.0

I'm surprised I haven't seen:
300

Here's a function that:
* rounds up and down
* works for both integers and floats
* is only two operations (as opposed to 3 in the solutions given above)
.... return n - n%k
........ round(167, 100)
100.... )
-300.... round(167, -100)
200.... round(500, -100)
500.... round(100.5, -100)
200.0100.0
 
A

Alec Taylor

o_O

Very nice

I'm surprised I haven't seen:

300

Here's a function that:
* rounds up and down
* works for both integers and floats
* is only two operations (as opposed to 3 in the solutions given above)

...     return n - n%k
...
... round(167, 100)
100
... )
-300
... round(167, -100)
200
... round(500, -100)
500
... round(100.5, -100)
200.0
100.0
 
N

noydb

I'm surprised I haven't seen:


300

Here's a function that:
* rounds up and down
* works for both integers and floats
* is only two operations (as opposed to 3 in the solutions given above)


...     return n - n%k
...>>> # Round down with a positive k:

... round(167, 100)
100>>> round(-233, 100

... )
-300>>> # Round up with a negative k:

... round(167, -100)
200>>> round(-233, -100)
-200

... round(500, -100)
500>>> round(500, 100)
500

... round(100.5, -100)
200.0>>> round(199.5, 100)

100.0

Thanks! Covers all bases, good.
 
H

Hrvoje Niksic

Terry Reedy said:

Note that that doesn't work for numbers that are already round:
3400 # 3300 would be correct

I'd go with Chris Rebert's (x + 99) // 100.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top