Skip to content

[bigint] casting breaks with strings containing thousands separators (commas) #25368

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

[bigint] '1,000'

Note:

  • The root cause is the specification of incompatible NumberStyle flags here: It uses NumberStyles.AllowDecimalPoint and NumberStyles.AllowExponent and NumberStyles.AllowLeadingSign, none of which may be combined with NumberStyles.AllowHexSpecifier

  • In its current form, the linked call is pointless altogether: While the ostensible fix is to use only NumberStyles.AllowHexSpecifier, this would parse something like '10' as 16 (e.g., [bigint]::parse('10', 'allowhexspecifier'))

  • Omitting the call would mean that values with , are handled via the fallback to ConvertNumericThroughDouble(), as for the primitive integer types, but note that this can result in a loss of precision:

    • As an aside: This loss of precision already occurs due to the initial parsing via TryScanNumber() treating larger-than-[decimal] numbers as [double]; e.g., [bigint] '99999999999999999999999999999' yields 99999999999999991433150857216(!); see also:

    • Similarly, the ConvertNumericThroughDouble() fallback is problematic for [long] and [ulong] casts with integer string values that contain , (which the initial TryScanNumber() call doesn't recognize), e.g., [long] '9223372036854775,807' fails, because the [double] detour results in a value too large for a [long] (the input value is, in effect, [long]::MaxValue), and something like [long] '9223372036854775,000' results in a loss of precision (yields 9223372036854774784)

Expected behavior

1000

Actual behavior

An exception occurs:

InvalidArgument: Cannot convert value "1,000" to type "System.Numerics.BigInteger". 
Error: "With the AllowHexSpecifier bit set in the enum bit field, the only other valid bits
that can be combined into the enum value must be a subset of those in HexNumber. (Parameter 'style')"

Error details

Environment data

PowerShell 7.5.0, 7.6.0-preview.3

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions