Add support for thousands separators in [bigint] casting (#25396)

This commit is contained in:
(Havox)
2025-05-08 17:10:33 +05:00
committed by GitHub
parent 7d6b6d8bf3
commit 1b786d2d89
2 changed files with 42 additions and 10 deletions
@@ -202,7 +202,6 @@ namespace System.Management.Automation
string sourceAsString = (string)LanguagePrimitives.ConvertTo(sourceValue, typeof(string), formatProvider);
return LanguagePrimitives.ConvertTo(sourceAsString, destinationType, formatProvider);
}
/// <summary>
/// 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 @@ namespace System.Management.Automation
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);
}
@@ -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
}
}