diff --git a/src/System.Management.Automation/engine/LanguagePrimitives.cs b/src/System.Management.Automation/engine/LanguagePrimitives.cs index ed1dc189270..6644223d7c6 100644 --- a/src/System.Management.Automation/engine/LanguagePrimitives.cs +++ b/src/System.Management.Automation/engine/LanguagePrimitives.cs @@ -202,7 +202,6 @@ public override object ConvertFrom(object sourceValue, Type destinationType, IFo string sourceAsString = (string)LanguagePrimitives.ConvertTo(sourceValue, typeof(string), formatProvider); return LanguagePrimitives.ConvertTo(sourceAsString, destinationType, formatProvider); } - /// /// Returns false, since this converter is not designed to be used to /// convert from the type associated with the converted to other types. @@ -2943,17 +2942,12 @@ private static object ConvertStringToInteger( return result; } - if (resultType == typeof(BigInteger)) - { - // Fallback for BigInteger: manual parsing using any common format. - NumberStyles style = NumberStyles.AllowLeadingSign - | NumberStyles.AllowDecimalPoint - | NumberStyles.AllowExponent - | NumberStyles.AllowHexSpecifier; - + if (resultType == typeof(BigInteger)) + { + NumberStyles style = NumberStyles.Integer | NumberStyles.AllowThousands; + return BigInteger.Parse(strToConvert, style, NumberFormatInfo.InvariantInfo); } - // Fallback conversion for regular numeric types. return GetIntegerSystemConverter(resultType).ConvertFrom(strToConvert); } diff --git a/test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 b/test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 index bc9d812afe4..6149adc2ab7 100644 --- a/test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 +++ b/test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 @@ -185,4 +185,42 @@ Describe "Language Primitive Tests" -Tags "CI" { $test.TestHandlerReturnEnum() | Should -BeTrue $test.TestHandlerReturnObject() | Should -BeTrue } + + It 'Handles large numbers with thousands separators that previously failed' { + $formattedNumber = "9223372036854775,807" + $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) + $convertedValue | Should -Be 9223372036854775807 + } + + It 'Handles extremely large numbers to verify precision' { + $formattedNumber = "99999999999999999999999999999" + $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) + $convertedValue | Should -Be 99999999999999999999999999999 + } + + It 'Parses mixed separators correctly' { + $formattedNumber = "1,0000,00" + $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) + $convertedValue | Should -Be 1000000 + } + + It 'Parses a number string using the invariant culture, irrespective of the current culture' { + $originalCulture = [cultureinfo]::CurrentCulture + try { + [cultureinfo]::CurrentCulture = [cultureinfo]::GetCultureInfo("de-DE") + $formattedNumber = "1.000" # in de-DE this means 1000 + $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) + # since [bigint] uses invariant culture, this will be parsed as 1 + $convertedValue | Should -Be 1 + } + finally { + [cultureinfo]::CurrentCulture = $originalCulture + } + } + + It 'Casts from floating-point number string to BigInteger using fallback' { + $formattedNumber = "1.2" + $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) + $convertedValue | Should -Be 1 + } }