Nicolas Gervais
Nicolas Gervais

Reputation: 36604

Round number with NumPy to the nearest thousand, but downward only

I am looking for the equivalent of np.round(234_567, -3), which would yield Out[1]: 235000. However, I want to round downward only. My desired output is: Out[1]: 234000.

import numpy as np

number = 234_567  # find a way to round this downward to 234_000

Upvotes: 0

Views: 1377

Answers (1)

Amadan
Amadan

Reputation: 198324

round rounds to nearest even; to round down, use floor instead. However, since it doesn't have the decimals parameter, you need to do that bit yourself:

np.floor(234_567 / 1_000) * 1_000

or, equivalently

np.floor(234_567 / 10 ** 3) * 10 ** 3

Upvotes: 2

Related Questions