Humanizr/Humanizer · error · ArgumentOutOfRangeException

Fractional seconds support only midpoint rounding ToEven and

Error message

Fractional seconds support only midpoint rounding ToEven and AwayFromZero.

What it means

Thrown by ValidateFractionalSecondArguments when the roundingMode parameter is neither MidpointRounding.ToEven nor MidpointRounding.AwayFromZero. Fractional-second rounding in Humanizer only supports these two deterministic midpoint strategies; others like ToPositiveInfinity or ToZero would produce locale-inconsistent results.

Source

Thrown at src/Humanizer/TimeSpanHumanizeExtensions.cs:350

            TimeUnit.Second,
            collectionSeparator,
            false,
            toSymbols);
    }

    internal static void ValidateFractionalSecondArguments(
        int maxFractionalDigits,
        MidpointRounding roundingMode,
        TimeUnit maxUnit)
    {
        if (maxFractionalDigits is < 0 or > 7)
        {
            throw new ArgumentOutOfRangeException(nameof(maxFractionalDigits));
        }

        if (roundingMode is not (MidpointRounding.ToEven or MidpointRounding.AwayFromZero))
        {
            throw new ArgumentOutOfRangeException(
                nameof(roundingMode),
                roundingMode,
                "Fractional seconds support only midpoint rounding ToEven and AwayFromZero.");
        }

        if (maxUnit is < TimeUnit.Second or > TimeUnit.Year)
        {
            throw new ArgumentOutOfRangeException(nameof(maxUnit));
        }
    }

    static RoundedTimeSpan RoundToFractionalSecondPrecision(
        TimeSpan timeSpan,
        int maxFractionalDigits,
        MidpointRounding roundingMode)
    {
        var magnitudeTicks = Math.Abs((decimal)timeSpan.Ticks);
        var quantumTicks = DecimalPowersOfTen[7 - maxFractionalDigits];

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Use MidpointRounding.AwayFromZero or MidpointRounding.ToEven only.
  2. Validate the rounding mode before the call and default to ToEven if unsupported.
  3. Review the fractional-seconds API documentation for the two supported modes.

Example fix

// before
var s = ts.HumanizeWithFractionalSeconds(3, 3, MidpointRounding.ToZero);

// after
var mode = roundingMode is MidpointRounding.ToEven or MidpointRounding.AwayFromZero
    ? roundingMode
    : MidpointRounding.ToEven;
var s = ts.HumanizeWithFractionalSeconds(3, 3, mode);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidRoundingMode(MidpointRounding mode) =>
    mode is MidpointRounding.ToEven or MidpointRounding.AwayFromZero;

Prevention

When it happens

Trigger: Calling any fractional-seconds humanize method with MidpointRounding.ToNegativeInfinity, ToPositiveInfinity, or ToZero as the roundingMode argument.

Common situations: Developer passes a different MidpointRounding value expecting it to work. Copying a rounding mode from a different API. Enum cast from an integer that doesn't map to the two supported values.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/4ec4c16de97b49e2. Report an issue: GitHub.