math.ceil(x) #無條件進位
四捨五入
class MathClass():
@classmethod
def get_round_half_up(cls, value):
import decimal
decimal.getcontext().rounding = "ROUND_HALF_UP"
dec_value = decimal.Decimal(str(value)).quantize(decimal.Decimal('0.00'))
dec_value = dec_value.quantize(decimal.Decimal('0.0'))
dec_value = dec_value.quantize(decimal.Decimal('0'))
int_value = int(dec_value)
print('{:f} = {:d}'.format(value, int_value))
return int_value
Usage
from MathClass import MathClass
def main():
int_value = MathClass.get_round_half_up(3.445) # 4
int_value = MathClass.get_round_half_up(3.444) # 3
if __name__ == '__main__':
main()
Output
3.445000 = 4
3.444000 = 3