Skip to content

Commit

Permalink
fix: Fixed double conversion exceptions as conversion depends on the …
Browse files Browse the repository at this point in the history
…user's culture...
  • Loading branch information
macmade committed Dec 20, 2023
1 parent ea518fa commit d69a3dc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ValueTransformers/Converters/ByAdding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ByAdding: MarkupExtension, IValueConverter
parameter = 0;
}

double n = System.Convert.ToDouble( value ) + System.Convert.ToDouble( parameter );
double n = Helper.ParseDouble( value ) + Helper.ParseDouble( parameter );

if( targetType == typeof( char ) )
{
Expand Down
2 changes: 1 addition & 1 deletion ValueTransformers/Converters/BySubtracting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class BySubtracting: MarkupExtension, IValueConverter
parameter = 0;
}

return new ByAdding().Convert( value, targetType, -System.Convert.ToDouble( parameter ), culture );
return new ByAdding().Convert( value, targetType, -Helper.ParseDouble( parameter ), culture );
}

public object? ConvertBack( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture )
Expand Down
2 changes: 1 addition & 1 deletion ValueTransformers/Converters/DividedBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DividedBy: MarkupExtension, IValueConverter
parameter = 1;
}

return new MultipliedBy().Convert( value, targetType, 1.0 / System.Convert.ToDouble( parameter ), culture );
return new MultipliedBy().Convert( value, targetType, 1.0 / Helper.ParseDouble( parameter ), culture );
}

public object? ConvertBack( object? value, Type targetType, object? parameter, System.Globalization.CultureInfo? culture )
Expand Down
10 changes: 10 additions & 0 deletions ValueTransformers/Converters/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,15 @@ public static bool IsZero( object? value )

throw new ArgumentException( "Unsupported type", nameof( value ) );
}

public static double ParseDouble( object? o )
{
if( o is string str )
{
return double.Parse( str, System.Globalization.CultureInfo.InvariantCulture );
}

return Convert.ToDouble( o );
}
}
}
2 changes: 1 addition & 1 deletion ValueTransformers/Converters/MultipliedBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class MultipliedBy: MarkupExtension, IValueConverter
parameter = 1;
}

double n = System.Convert.ToDouble( value ) * System.Convert.ToDouble( parameter );
double n = Helper.ParseDouble( value ) * Helper.ParseDouble( parameter );

if( targetType == typeof( char ) )
{
Expand Down

0 comments on commit d69a3dc

Please sign in to comment.