Cole MG
Cole MG

Reputation: 331

Round up to the nearest multiple of .5 python

How do I round up to the nearest increment of .5? Say I have

a=2.3

and want to declare b as 2.5 taking a as an input and rounding up to the nearest .5

Upvotes: 1

Views: 114

Answers (2)

Kelly Bundy
Kelly Bundy

Reputation: 27588

You could add what's missing.

a += -a % .5

Upvotes: 1

esqew
esqew

Reputation: 44699

You can use a combination of math.ceil and simple arithmetic operations to get where you need:

import math
a = 2.3
rounded_a = math.ceil(a * 2) / 2
print(rounded_a) # 2.5

Upvotes: 1

Related Questions