michael-lazar / astrobotany

🌱 A community garden over the Gemini protocol
gemini://astrobotany.mozz.us
Other
132 stars 11 forks source link

Better formatting for growth_rate #64

Closed AbstractBeliefs closed 3 years ago

AbstractBeliefs commented 3 years ago

The way that growth_rate is calculated in models.py allows for float representation issues to creep in, which are then propagated to your plants info view when via views.py . Adjusting the format specification allows us to limit output to a single decimal place.

>>> growth_rate = 1 + 0.2 * (13-1)   # per models.py
>>> growth_rate
3.4000000000000004
>>> f"Growth Rate: {growth_rate}"    # per views.py
'Growth Rate: 3.4000000000000004'
>>> f"Growth Rate: {growth_rate:#.2}"
'Growth Rate: 3.4'
AbstractBeliefs commented 3 years ago

(I'm quite sure that test failure isn't anything to do with this change)

michael-lazar commented 3 years ago

Thanks! What does the # do in the format string? Normally I would see this written as f"{growth_rate:.2}"

AbstractBeliefs commented 3 years ago

It's used to force a decimal point, but I don't think in this context it's important - it can be removed. https://docs.python.org/3/library/string.html#format-specification-mini-language