Humanizr/Humanizer · error · FormatException
Value is not in the correct format
Error message
Value is not in the correct format
What it means
Thrown by ByteSize.ParseWithUnitSystem when TryParseSpanWithUnitSystem returns false for the supplied text under the chosen unit system. ParseWithUnitSystem is the throwing wrapper over the Try variant; the FormatException signals that the input did not match the grammar or unit tokens of that system.
Source
Thrown at src/Humanizer/Bytes/ByteSize.cs:1543
/// For <see cref="ByteSizeUnitSystem.DecimalSi"/> and <see cref="ByteSizeUnitSystem.BinaryIec"/>, SI/IEC-prefixed
/// unit tokens are matched case-insensitively, while <c>b</c> and <c>B</c> remain case-sensitive.
/// Legacy parsing preserves the established behavior.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="unitSystem"/> is not defined.</exception>
/// <exception cref="FormatException"><paramref name="s"/> is not valid for the selected unit system.</exception>
public static ByteSize ParseWithUnitSystem(
string s,
ByteSizeUnitSystem unitSystem,
IFormatProvider? formatProvider = null)
{
ArgumentNullException.ThrowIfNull(s);
if (TryParseSpanWithUnitSystem(s.AsSpan(), unitSystem, formatProvider, out var result))
{
return result;
}
throw new FormatException("Value is not in the correct format");
}
/// <summary>
/// Attempts to parse a byte size using only the tokens defined by the selected unit system.
/// </summary>
/// <param name="s">The text to parse.</param>
/// <param name="unitSystem">The unit system whose tokens are accepted.</param>
/// <param name="result">
/// When this method returns, contains the parsed byte size if parsing succeeded; otherwise, the default value.
/// </param>
/// <returns><see langword="true"/> if parsing succeeded; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// For <see cref="ByteSizeUnitSystem.DecimalSi"/> and <see cref="ByteSizeUnitSystem.BinaryIec"/>, SI/IEC-prefixed
/// unit tokens are matched case-insensitively, while <c>b</c> and <c>B</c> remain case-sensitive.
/// Legacy parsing preserves the established behavior.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="unitSystem"/> is not defined.</exception>
public static bool TryParseWithUnitSystem(View on GitHub (pinned to ffc2b77c0f)
Solutions
- Prefer TryParseWithUnitSystem and report a friendly validation error on false rather than letting Parse throw.
- Confirm the unit token in the input belongs to the selected system (decimal vs binary) before parsing.
- Supply a formatProvider whose NumberDecimalSeparator matches the input's locale.
Example fix
// before
var size = ByteSize.ParseWithUnitSystem(input, ByteSizeUnitSystem.DecimalSi);
// after
if (!ByteSize.TryParseWithUnitSystem(input, ByteSizeUnitSystem.DecimalSi, out var size))
{
ModelState.AddModelError(nameof(input), "Enter a value like '1.5 GB' using decimal SI units.");
return;
} Defensive patterns
Strategy: validation
Validate before calling
if (!ByteSize.TryParseWithUnitSystem(input, system, out var size))
throw new FormatException($"Cannot parse '{input}' with the selected unit system."); Try / catch
try { return ByteSize.ParseWithUnitSystem(input, system); }
catch (FormatException) { return null; } Prevention
- Use TryParseWithUnitSystem at trust boundaries
- Match the unit token to the selected system
- Supply a formatProvider whose separators match the input
When it happens
Trigger: Calling ParseWithUnitSystem("1234 XYZ", ByteSizeUnitSystem.DecimalSi) where XYZ is not a recognized decimal token, or passing a binary 'GiB' token to a DecimalSi parse, or supplying text that has no parseable number. Empty/whitespace strings and stray symbols also fail.
Common situations: Parsing user-typed capacity strings, ingesting localized values whose decimal separator does not match the supplied formatProvider, or unit-system mismatches where the source data uses one convention but the parser is locked to the other.
Related errors
- Unit token '{unit.Symbol}' does not belong to the selected b
- A byte-size format can contain only one distinct unit token.
- Unknown byte-size unit system.
- The result is outside the range supported by ByteSize.Bits.
- Unknown byte-size unit system.
AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13).
Data as JSON: /api/errors/f692b54bf9e23cef.
Report an issue: GitHub.