Blankman
Blankman

Reputation: 267320

How to round to the next 1000 value?

I want to round to the next 1000 value, always rounding up.

Example inputs:

1 -> 1000
440 -> 1000
1001 -> 2000
14399 -> 15000
108 030 -> 109 000

I can only find a Float.ceil function which is not relevant for me.

Upvotes: 1

Views: 277

Answers (2)

Train
Train

Reputation: 3516

Ciel is correct, you need to add some math to it.

ceil(number/1000)*1000

Edit: To round to the nearest number you want

 ceil(number/n)*n //n = number to round to

Upvotes: 7

Adam Millerchip
Adam Millerchip

Reputation: 23147

Train's answer is great (duly upvoted). You could also make a function, to allow you to specify the rounding target:

def ceil_nearest(num, target), do: ceil(num/target)*target

Then:

ceil_nearest(14399, 1000) # 15000
ceil_nearest(14399, 500)  # 14500

Upvotes: 3

Related Questions