Add round to half template method (#28948)
* added round to half method Signed-off-by: Tobias Perschon <tobias@perschon.at> * testcase for new round method Signed-off-by: Tobias Perschon <tobias@perschon.at>pull/28958/head
parent
6d77c15f28
commit
d92f48646a
|
@ -669,6 +669,8 @@ def forgiving_round(value, precision=0, method="common"):
|
||||||
value = math.ceil(float(value) * multiplier) / multiplier
|
value = math.ceil(float(value) * multiplier) / multiplier
|
||||||
elif method == "floor":
|
elif method == "floor":
|
||||||
value = math.floor(float(value) * multiplier) / multiplier
|
value = math.floor(float(value) * multiplier) / multiplier
|
||||||
|
elif method == "half":
|
||||||
|
value = round(float(value) * 2) / 2
|
||||||
else:
|
else:
|
||||||
# if method is common or something else, use common rounding
|
# if method is common or something else, use common rounding
|
||||||
value = round(float(value), precision)
|
value = round(float(value), precision)
|
||||||
|
|
|
@ -227,6 +227,13 @@ def test_rounding_value(hass):
|
||||||
== "12.8"
|
== "12.8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert (
|
||||||
|
template.Template(
|
||||||
|
'{{ states.sensor.temperature.state | round(1, "half") }}', hass
|
||||||
|
).async_render()
|
||||||
|
== "13.0"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_rounding_value_get_original_value_on_error(hass):
|
def test_rounding_value_get_original_value_on_error(hass):
|
||||||
"""Test rounding value get original value on error."""
|
"""Test rounding value get original value on error."""
|
||||||
|
|
Loading…
Reference in New Issue