Skip to content

ui::IncrementNumberRounder

Header: winui/core/NumberFormat.hpp.

Rounds to a multiple of increment, which must be positive.

Set as DecimalFormatter::rounder, it changes only the displayed text. A NumberBox keeps the unrounded value.

Example:

// Cash amounts in Swiss francs go to the nearest 5 cents.
const ui::IncrementNumberRounder cash{0.05, ui::RoundingAlgorithm::RoundHalfUp};
const double due = cash.round(12.33); // 12.35

// As a formatter's rounder it shapes what a NumberBox or a label shows.
const ui::DecimalFormatter francs{.fraction_digits = 2, .rounder = cash};
window.set_content(ui::Text{.text = "Due: CHF " + francs.format(12.33)}); // "Due: CHF 12.35"

// Banker's rounding sends a tie to the even neighbor, so ties don't drift a sum upward.
const ui::IncrementNumberRounder bankers{1.0, ui::RoundingAlgorithm::RoundHalfToEven};
const double whole = bankers.round(2.5) + bankers.round(3.5); // 2 + 4

increment

double increment = 1.0

The step results are a multiple of; must be positive. 0.01 rounds to cents.

algorithm

RoundingAlgorithm algorithm = RoundingAlgorithm::RoundHalfUp

How ties and non-multiples are resolved; half-up by default.

round

double round(double value) const

value rounded to a neighboring multiple of increment, picked by algorithm. The result is cleaned to 15 significant digits, so 0.1-steps land on 0.3, not 0.30000000000000004.