Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

decimal.ROUND_HALF_UP — ties away from zero

decimal.ROUND_HALF_UP selects the nearest result and rounds exact ties away from zero, including negative values.

Version: Python 3.14; repository image, production runtime not attested

Syntax

value.quantize(Decimal('0.01'), rounding=decimal.ROUND_HALF_UP)

decimal.ROUND_HALF_UP is a rounding-mode constant, not a function. It selects the nearest value; exactly halfway, it selects the value farther from zero. It does not mean always rounding toward positive infinity.

Pass it to quantize() as rounding. The template Decimal('0.01') specifies two decimal places. Construct values from strings: conversion from float preserves its binary approximation.

Unlike ROUND_UP, this mode does not always round away from zero. ROUND_HALF_EVEN resolves ties by choosing an even last retained digit. Context precision still limits the result.

Examples

import decimal

context = decimal.Context(prec=10)
for text in ('1.234', '1.235', '-1.235'):
    value = decimal.Decimal(text)
    result = value.quantize(
        decimal.Decimal('0.01'),
        rounding=decimal.ROUND_HALF_UP,
        context=context,
    )
    print(f'{text} -> {result}')

Result

1.234 -> 1.23
1.235 -> 1.24
-1.235 -> -1.24

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources