Humanizr/Humanizer · error · ArgumentOutOfRangeException

Precision must be greater than zero.

Error message

Precision must be greater than zero.

What it means

Thrown by ByteSizeExtensions.HumanizeComposite when the precision argument is less than one. Precision controls the maximum number of non-zero parts (e.g. '1 GB 2 MB' = precision 2); zero or negative precision is meaningless and is rejected before any formatting begins.

Source

Thrown at src/Humanizer/Bytes/ByteSizeExtensions.cs:503

    /// </summary>
    /// <param name="input">The byte quantity to humanize.</param>
    /// <param name="precision">The maximum number of non-zero parts to return.</param>
    /// <param name="formatProvider">The format provider to use. If null, the current culture is used.</param>
    /// <param name="separator">The separator to use between parts.</param>
    /// <param name="toWords">Uses unit words instead of symbols if true.</param>
    /// <returns>The composite byte quantity.</returns>
    /// <exception cref="ArgumentOutOfRangeException"><paramref name="precision"/> is less than one.</exception>
    /// <exception cref="ArgumentNullException"><paramref name="separator"/> is null.</exception>
    public static string HumanizeComposite(
        this ByteSize input,
        int precision = 2,
        IFormatProvider? formatProvider = null,
        string separator = " ",
        bool toWords = false)
    {
        if (precision < 1)
        {
            throw new ArgumentOutOfRangeException(nameof(precision), precision, "Precision must be greater than zero.");
        }

        ArgumentNullException.ThrowIfNull(separator);

        formatProvider ??= CultureInfo.CurrentCulture;
        var culture = formatProvider as CultureInfo;
        if (culture is not null)
        {
            formatProvider = LocaleNumberFormattingOverrides.GetFormattingNumberFormat(culture);
        }

        var formatter = Configurator.GetFormatter(culture);
        var isNegative = input.Bits < 0;
        var remainingBits = isNegative
            ? (ulong)(-(input.Bits + 1)) + 1
            : (ulong)input.Bits;

        if (remainingBits == 0)

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Clamp precision to at least 1 before calling: Math.Max(1, requestedPrecision).
  2. Validate user-supplied precision at the input boundary and reject values below one.
  3. Use the default (2) unless a specific part count is genuinely needed.

Example fix

// before
var text = size.HumanizeComposite(precision: userPrecision);

// after
var text = size.HumanizeComposite(precision: Math.Max(1, userPrecision));
Defensive patterns

Strategy: validation

Validate before calling

if (precision < 1) throw new ArgumentOutOfRangeException(nameof(precision));

Prevention

When it happens

Trigger: Calling HumanizeComposite(precision: 0), HumanizeComposite(precision: -1), or passing a computed precision that underflows to zero (e.g. a count minus a buffer).

Common situations: Binding precision to a user setting with no minimum, deriving precision from a list length that can be zero, or copy-pasting a default argument and zeroing it out.

Related errors


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