你已经派生过 gprMax
镜像自地址
https://gitee.com/sunhf/gprMax.git
已同步 2025-08-08 07:24:19 +08:00
Create specific int and float rounding functions
这个提交包含在:
@@ -141,6 +141,33 @@ def logo(version):
|
|||||||
return str
|
return str
|
||||||
|
|
||||||
|
|
||||||
|
def round_int(value: float) -> int:
|
||||||
|
"""Round number to nearest integer (half values are rounded down).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value: Number to round.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
rounded: Rounded value.
|
||||||
|
"""
|
||||||
|
return int(d.Decimal(value).quantize(d.Decimal("1"), rounding=d.ROUND_HALF_DOWN))
|
||||||
|
|
||||||
|
|
||||||
|
def round_float(value: float, decimalplaces: int) -> float:
|
||||||
|
"""Round down to a specific number of decimal places.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value: Number to round.
|
||||||
|
decimalplaces: Number of decimal places of float to represent
|
||||||
|
rounded value.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
rounded: Rounded value.
|
||||||
|
"""
|
||||||
|
precision = f"1.{'0' * decimalplaces}"
|
||||||
|
return float(d.Decimal(value).quantize(d.Decimal(precision), rounding=d.ROUND_FLOOR))
|
||||||
|
|
||||||
|
|
||||||
def round_value(value: float, decimalplaces: int = 0) -> Union[float, int]:
|
def round_value(value: float, decimalplaces: int = 0) -> Union[float, int]:
|
||||||
"""Rounding function.
|
"""Rounding function.
|
||||||
|
|
||||||
@@ -155,12 +182,11 @@ def round_value(value: float, decimalplaces: int = 0) -> Union[float, int]:
|
|||||||
|
|
||||||
# Rounds to nearest integer (half values are rounded downwards)
|
# Rounds to nearest integer (half values are rounded downwards)
|
||||||
if decimalplaces == 0:
|
if decimalplaces == 0:
|
||||||
rounded = int(d.Decimal(value).quantize(d.Decimal("1"), rounding=d.ROUND_HALF_DOWN))
|
rounded = round_int(value)
|
||||||
|
|
||||||
# Rounds down to nearest float represented by number of decimal places
|
# Rounds down to nearest float represented by number of decimal places
|
||||||
else:
|
else:
|
||||||
precision = f"1.{'0' * decimalplaces}"
|
rounded = round_float(value, decimalplaces)
|
||||||
rounded = float(d.Decimal(value).quantize(d.Decimal(precision), rounding=d.ROUND_FLOOR))
|
|
||||||
|
|
||||||
return rounded
|
return rounded
|
||||||
|
|
||||||
|
在新工单中引用
屏蔽一个用户