From 7916e106a69d3c540f44fb3daee08f7c578bb13b Mon Sep 17 00:00:00 2001 From: Solal Pirelli Date: Thu, 29 May 2025 09:49:53 +0200 Subject: [PATCH] Add note on BigInteger bin/hex formatting of positive values See dotnet/runtime#115618. It's somewhat unexpected that "print `3` with 2 binary digits" returns a string of length 3 (`"011"`), but also makes sense given the round-trip requirement and the historical context of not using `-` for negative bin/hex numbers. I think documenting this behavior would be useful. --- docs/standard/base-types/standard-numeric-format-strings.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/standard/base-types/standard-numeric-format-strings.md b/docs/standard/base-types/standard-numeric-format-strings.md index 2d88f76072324..d8ab53813c07e 100644 --- a/docs/standard/base-types/standard-numeric-format-strings.md +++ b/docs/standard/base-types/standard-numeric-format-strings.md @@ -98,6 +98,9 @@ The binary ("B") format specifier converts a number to a string of binary digits The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. +For instance, the number `3` converted with the format specifier `"B2"` is `011` because the binary number `11` represents the negative value `-1`. + The result string is not affected by the formatting information of the current object. @@ -309,6 +312,9 @@ The hexadecimal ("X") format specifier converts a number to a string of hexadeci The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. +For , positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed. +For instance, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`. + The result string is not affected by the formatting information of the current object. The following example formats values with the hexadecimal format specifier.