Skip to content

Commit 9360964

Browse files
Fix Remove commas from numeric strings before conversion to prevent casting errors… (PowerShell#25396)
1 parent 0eb8b52 commit 9360964

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,14 @@ public override bool CanConvertFrom(object sourceValue, Type destinationType)
199199
/// <exception cref="PSInvalidCastException">When no conversion was possible.</exception>
200200
public override object ConvertFrom(object sourceValue, Type destinationType, IFormatProvider formatProvider, bool ignoreCase)
201201
{
202-
string sourceAsString = (string)LanguagePrimitives.ConvertTo(sourceValue, typeof(string), formatProvider);
203-
return LanguagePrimitives.ConvertTo(sourceAsString, destinationType, formatProvider);
202+
string sourceAsString = LanguagePrimitives.ConvertTo(sourceValue, typeof(string), formatProvider) as string;
203+
if (sourceAsString != null){
204+
string groupSeparator = formatProvider?.GetFormat(typeof(NumberFormatInfo)) is NumberFormatInfo nfi
205+
? nfi.NumberGroupSeparator
206+
: CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator;
207+
sourceAsString = sourceAsString.Replace(groupSeparator, string.Empty); // Remove culture-specific group separator
208+
}
209+
return LanguagePrimitives.ConvertTo(sourceAsString, destinationType, formatProvider);
204210
}
205211

206212
/// <summary>

0 commit comments

Comments
 (0)