Reputation: 267320
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
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
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