bia-fitness/common.py

21 lines
588 B
Python
Raw Permalink Normal View History

2015-04-10 16:28:25 +00:00
import decimal
def remove_exponent(d):
return d.quantize(decimal.Decimal(1)) if d == d.to_integral() else d.normalize()
def round_to(number, increment, up_threshold=0.95):
""" round number to nearest multiple of increment """
ratio = number/increment
whole = ratio.to_integral(rounding=decimal.ROUND_DOWN)
dec = ratio - whole
if dec >= up_threshold:
whole += 1
return whole * increment
def to_lb(weight_kg):
return remove_exponent(round_to(weight_kg * decimal.Decimal("2.2046"),
decimal.Decimal("0.125")))