{"record":{"id":"ee00e1971ec0bcc1","repo":"Humanizr/Humanizer","slug":"unit-token-unit-symbol-does-not-belong-to-the","errorCode":null,"errorMessage":"Unit token '{unit.Symbol}' does not belong to the selected byte-size unit system.","messagePattern":"Unit token '(.+?)' does not belong to the selected byte-size unit system\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/Bytes/ByteSize.cs","lineNumber":1028,"sourceCode":"        return absoluteBytes >= 1\n            ? new(1, ByteSymbol, DataUnit.Byte)\n            : new(1d / BitsInByte, BitSymbol, DataUnit.Bit);\n    }\n\n    static SystemUnit? FindFormatUnit(string? format, SystemUnit[] units)\n    {\n        if (string.IsNullOrWhiteSpace(format) || format == \"G\")\n        {\n            return null;\n        }\n\n        var maskedFormat = MaskFormatLiterals(format!);\n        var incompatibleUnits = ReferenceEquals(units, DecimalUnits) ? BinaryUnits : DecimalUnits;\n        foreach (var unit in incompatibleUnits)\n        {\n            if (maskedFormat.Contains(unit.Symbol, StringComparison.OrdinalIgnoreCase))\n            {\n                throw new FormatException($\"Unit token '{unit.Symbol}' does not belong to the selected byte-size unit system.\");\n            }\n        }\n\n        if (maskedFormat.Contains(\"EiB\", StringComparison.OrdinalIgnoreCase))\n        {\n            throw new FormatException(\"EiB is outside the range supported by ByteSize.Bits.\");\n        }\n\n        SystemUnit? selectedUnit = null;\n        var remainingFormat = maskedFormat;\n        foreach (var unit in units)\n        {\n            if (!remainingFormat.Contains(unit.Symbol, StringComparison.OrdinalIgnoreCase))\n            {\n                continue;\n            }\n\n            if (selectedUnit is not null && selectedUnit.Value.DataUnit != unit.DataUnit)","sourceCodeStart":1010,"sourceCodeEnd":1046,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/Bytes/ByteSize.cs#L1010-L1046","documentation":"Thrown by ByteSize.FindFormatUnit while interpreting a format string for a chosen unit system. After masking quoted/escaped literals, if the format contains a symbol belonging to the opposite system (e.g. a binary 'GiB' token while formatting with DecimalSi units, or a decimal 'GB' while using BinaryIec), the library refuses to silently mis-render it.","triggerScenarios":"Calling ToString(\"#.## GiB\", ...) on a ByteSize whose current unit system is DecimalSi, or calling Humanize with a format string that mixes 'MB' into a BinaryIec format. The masked format is scanned case-insensitively against the incompatible unit list.","commonSituations":"Copy-pasting a format string between code paths that use different unit systems, localizing UI where the format string comes from a resource file keyed to the wrong system, or upgrading to a version that enforces system-token separation that was previously lenient.","solutions":["Match the format string's unit token to the active unit system (decimal SI uses kB/MB/GB/TB/PB/EB; binary IEC uses KiB/MiB/GiB/TiB/PiB).","If you genuinely want both, render twice with separate unit systems and concatenate.","Quote literal text containing unit-like letters with single quotes (e.g. '#.## \"GiB\"') so MaskFormatLiterals hides them from the scan."],"exampleFix":"// before (DecimalSi context)\nvar text = size.ToString(\"#.## GiB\");\n\n// after\nvar text = size.ToString(\"#.## GB\"); // token matches DecimalSi\n// or, if GiB was intentional, switch the unit system first","handlingStrategy":"validation","validationCode":"static string NormalizeFormat(string format, ByteSizeUnitSystem system)\n{\n    var binary = new HashSet<string> { \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\" };\n    var decimalSi = new HashSet<string> { \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\" };\n    var disallowed = system == ByteSizeUnitSystem.BinaryIec ? decimalSi : binary;\n    foreach (var token in disallowed)\n        if (format.Contains(token, StringComparison.OrdinalIgnoreCase))\n            throw new ArgumentException($\"Format contains incompatible token {token}.\");\n    return format;\n}","typeGuard":null,"tryCatchPattern":"try { return size.ToString(format); }\ncatch (FormatException) { throw new ArgumentException(\"Format token does not match the active unit system.\", nameof(format)); }","preventionTips":["Match format tokens to the active unit system","Keep format strings in resources keyed by system","Quote literal unit-like text to hide it from the scanner"],"tags":["bytesize","format-string","unit-system","formatexception"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}