2

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.

4 Answers4

2

In Python, you can do

print((number // 1000)*1000)
Senthil
  • 189
  • 4
  • 16
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