can somebody tell me how i can round down to the nearest thousand. So far I tried it with math.round(), the truncate function but i couldn't find my math skills to work out for me. As a example for some people I want that 4520 ends up in beeing 4000.
Asked
Active
Viewed 214 times
2
-
`int(x/1000)*1000`? – not_speshal May 06 '22 at 14:47
-
24000 is not the **nearest** thousand of 4520, this would be 5000 – mozway May 06 '22 at 14:49
-
1@mozway - "how i can round *down* to the nearest thousand" – not_speshal May 06 '22 at 14:55
-
@not_speshal fair enough (but a duplicate in both cases) :p – mozway May 06 '22 at 14:57
4 Answers
2
just a thought
Why not do it the traditional way?!
deprecate_vals = 3
val = 4520
val = int(val/(10**deprecate_vals)) * (10**deprecate_vals)
print(val)

High-Octane
- 1,104
- 5
- 19
2
You can divide by 1000 before rounding and then multiply by 1000.
invalue = 4320
outvalue = 1000 * round(invalue/1000)
print("invalue: ",invalue)
print("rounded value: ",outvalue)
output
invalue: 4320
rounded value: 4000
1
There is multiple way to do it.
You can get the digit for the thousand by using the division (// mean without remaining, so you don't get 4,52 but 4)
x = 4520
rounded_x = (x//1000) * 1000
You can also use the remaining of the division with the modulo operator and remove it to the value :
x = 4520
rounded_x = x - (x%1000)

Xiidref
- 1,456
- 8
- 20