8000 Add support for thousands separators in [bigint] casting (#25396) · PowerShell/PowerShell@1b786d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b786d2

Browse files
Add support for thousands separators in [bigint] casting (#25396)
1 parent 7d6b6d8 commit 1b786d2

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/System.Management.Automation/engine/LanguagePrimitives.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ public override object ConvertFrom(object sourceValue, Type destinationType, IFo
202202
string sourceAsString = (string)LanguagePrimitives.ConvertTo(sourceValue, typeof(string), formatProvider);
203203
return LanguagePrimitives.ConvertTo(sourceAsString, destinationType, formatProvider);
204204
}
205-
206205
/// <summary>
207206
/// Returns false, since this converter is not designed to be used to
208207
/// convert from the type associated with the converted to other types.
@@ -2943,17 +2942,12 @@ private static object ConvertStringToInteger(
29432942
return result;
29442943
}
29452944

2946-
if (resultType == typeof(BigInteger))
2947-
{
2948-
// Fallback for BigInteger: manual parsing using any common format.
2949-
NumberStyles style = NumberStyles.AllowLeadingSign
2950-
| NumberStyles.AllowDecimalPoint
2951-
| NumberStyles.AllowExponent
2952-
| NumberStyles.AllowHexSpecifier;
2953-
2945+
if (resultType == typeof(BigInteger))
2946+
{
2947+
NumberStyles style = NumberStyles.Integer | NumberStyles.AllowThousands;
2948+
29542949
return BigInteger.Parse(strToConvert, style, NumberFormatInfo.InvariantInfo);
29552950
}
2956-
29572951
// Fallback conversion for regular numeric types.
29582952
return GetIntegerSystemConverter(resultType).ConvertFrom(strToConvert);
29592953
}

test/powershell/engine/Api/LanguagePrimitive.Tests.ps1

+38
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,42 @@ Describe "Language Primitive Tests" -Tags "CI" {
185185
$test.TestHandlerReturnEnum() | Should -BeTrue
186186
$test.TestHandlerReturnObject() | Should -BeTrue
187187
}
188+
189+
It 'Handles large numbers with thousands separators that previously failed' {
190+
$formattedNumber = "9223372036854775,807"
191+
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
192+
$convertedValue | Should -Be 9223372036854775807
193+
}
194+
195+
It 'Handles extremely large numbers to verify precision' {
196+
$formattedNumber = "99999999999999999999999999999"
197+
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
198+
$convertedValue | Should -Be 99999999999999999999999999999
199+
}
200+
201+
It 'Parses mixed separators correctly' {
202+
$formattedNumber = "1,0000,00"
203+
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
204+
$convertedValue | Should -Be 1000000
205+
}
206+
207+
It 'Parses a number string using the invariant culture, irrespective of the current culture' {
208+
$originalCulture = [cultureinfo]::CurrentCulture
209+
try {
210+
[cultureinfo]::CurrentCulture = [cultureinfo]::GetCultureInfo("de-DE")
211+
$formattedNumber = "1.000" # in de-DE this means 1000
212+
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
213+
# since [bigint] uses invariant culture, this will be parsed as 1
214+
$convertedValue | Should -Be 1
215+
}
216+
finally {
217+
[cultureinfo]::CurrentCulture = $originalCulture
218+
}
219+
}
220+
221+
It 'Casts from floating-point number string to BigInteger using fallback' {
222+
$formattedNumber = "1.2"
223+
$convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint])
224+
$convertedValue | Should -Be 1
225+
}
188226
}

0 commit comments

Comments
 (0)
0