{"record":{"id":"030e58b28348e03c","repo":"Humanizr/Humanizer","slug":"a-byte-size-format-can-contain-only-one-distinct-u","errorCode":null,"errorMessage":"A byte-size format can contain only one distinct unit token.","messagePattern":"A byte-size format can contain only one distinct unit token\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/Bytes/ByteSize.cs","lineNumber":1048,"sourceCode":"        }\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)\n            {\n                throw new FormatException(\"A byte-size format can contain only one distinct unit token.\");\n            }\n\n            selectedUnit = unit;\n            remainingFormat = ReplaceOrdinalIgnoreCase(\n                remainingFormat,\n                unit.Symbol,\n                new(' ', unit.Symbol.Length));\n        }\n\n        var containsBytes = remainingFormat.Contains(ByteSymbol, StringComparison.Ordinal);\n        var containsBits = remainingFormat.Contains(BitSymbol, StringComparison.Ordinal);\n        var tokenCount = (selectedUnit is null ? 0 : 1) + (containsBytes ? 1 : 0) + (containsBits ? 1 : 0);\n        if (tokenCount > 1)\n        {\n            throw new FormatException(\"A byte-size format can contain only one distinct unit token.\");\n        }\n\n        if (selectedUnit is not null)","sourceCodeStart":1030,"sourceCodeEnd":1066,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/Bytes/ByteSize.cs#L1030-L1066","documentation":"Thrown by ByteSize.FindFormatUnit when the masked format selects two distinct higher-order unit tokens with different DataUnit values (for example both 'MB' and 'GB' in the same decimal format, or 'MiB' and 'GiB' in a binary format). A byte-size format may name exactly one scaling unit.","triggerScenarios":"Passing a format string like \"#.## MB GB\" or \"# GiB MiB\" to ByteSize.ToString. The loop over the active unit array sets selectedUnit, and on the next match with a different DataUnit it throws.","commonSituations":"Building a format string by concatenating two template fragments, templating errors where a placeholder leaks a unit token, or attempting to render a ratio like 'X MB per Y GB' in a single ToString call instead of two.","solutions":["Use a single unit token per format string; render the second quantity in a separate ToString call.","If you need a ratio, compute the numbers yourself and interpolate them into a plain string.","Quote one of the tokens as a literal (\"MB\") if it is meant as decoration rather than the active unit."],"exampleFix":"// before\nvar text = $\"{used.ToString(\"#.## GB\")} / {total.ToString(\"#.## GB\")}\"; // accidentally merged into one format\n\n// after\nvar text = $\"{used.ToString(\"#.## GB\")} / {total.ToString(\"#.## GB\")}\"; // two separate calls","handlingStrategy":"validation","validationCode":"static void EnsureSingleScalingUnit(string format, SystemUnit[] units)\n{\n    var matches = units.Count(u => format.Contains(u.Symbol, StringComparison.OrdinalIgnoreCase));\n    if (matches > 1) throw new ArgumentException(\"Format contains more than one scaling unit token.\");\n}","typeGuard":null,"tryCatchPattern":"try { return size.ToString(format); }\ncatch (FormatException) { throw new ArgumentException(\"Use exactly one scaling unit token per format.\", nameof(format)); }","preventionTips":["One scaling unit per format string","Render ratios as two separate ToString calls","Quote extra unit-like letters as literals"],"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"}