Skip to content

Parse("10", NumberStyles.HexNumber) silently returns 10 instead of 16 (or rejecting the style): AllowHexSpecifier and AllowBinarySpecifier are ignored #177

Closed

Description

@matt-edmondson

What's wrong

Parse(ReadOnlySpan<char>, NumberStyles, IFormatProvider?) (PreciseNumber/PreciseNumber.cs:1200-1307) and TryParse (~1369) never look at NumberStyles.AllowHexSpecifier or NumberStyles.AllowBinarySpecifier (neither name appears in the file). Input that happens to be all decimal digits is parsed as decimal, so a wrong value comes back with no error.

static T ParseG<T>(string s, NumberStyles st) where T : INumberBase<T>
    => T.Parse(s, st, CultureInfo.InvariantCulture);

ParseG<PreciseNumber>("10", NumberStyles.HexNumber);      // 10   — BigInteger/int give 16; decimal/double throw ArgumentException
ParseG<PreciseNumber>("1010", NumberStyles.BinaryNumber); // 1010 — int gives 10
PreciseNumber.TryParse("10", NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var r); // true, r = 10
ParseG<PreciseNumber>("A", NumberStyles.HexNumber);       // FormatException

Reproduced against the net10.0 build at 907bd2d.

Why it matters

Generic-math code (T.Parse(s, NumberStyles.HexNumber, ...) over INumberBase<T>) gets a silently wrong number for PreciseNumber where every BCL type either gives the right answer or refuses the style. This is a gap left by the #132 fix (styles are now validated, but these two flags fall through). It's distinct from #152, which is about valid inputs being rejected.

Suggested fix

Either:

  • throw ArgumentException when either flag is set, matching decimal and double, or
  • support them for integer input, matching BigInteger.

Acceptance: the calls above either return 16 / 10 or throw ArgumentException (for both Parse and TryParse); add tests alongside the existing NumberStyles tests.

Activity

  1. matt-edmondson commented on Oct 29, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions