Skip to content

(int) cast of double rounds the double instead of truncating it #130

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Crown0815 opened this issue Feb 17, 2022 · 3 comments
Closed

(int) cast of double rounds the double instead of truncating it #130

Crown0815 opened this issue Feb 17, 2022 · 3 comments
Assignees
Labels

Comments

@Crown0815
Copy link

In C# the call

var x = (int) 3.6;

produces x = 3.

The same call in ExpressionEvaluator produces x = 4 instead.

This is due to the usage of

...
return Convert.ChangeType(value, conversionType);

in line 4150, which for the given example resolves to Convert.ChangeType(3.6, typeof(int)) which indeed resolves to 4.

I understand if this is a design decision, but it might be a source of issues. We use ExpressionEvaluator to simulate C# code, which means for us, it is non-expected behavior.

For our intents and purposes it would be great to have an option to turn integer casting from round to truncate e.g. with

evaluator.OptionTruncateOnIntegerCast = true;

This would preserve current behavior but enable a more correct simulation of C# behavior.

@codingseb codingseb self-assigned this Feb 17, 2022
@codingseb codingseb added the bug label Feb 17, 2022
@codingseb
Copy link
Owner

Hello @Crown0815 and thank you for this issue.
You are right it should return 3.
I think there is no need for an option for this as ExpressionEvaluator should behave as C#.
For me it's more a bug to correct.
I thought that Convert was working like a cast but I need to make a bit more test around this.

@codingseb
Copy link
Owner

I published version 1.4.37.0
That correct this bug

@Crown0815
Copy link
Author

@codingseb Amazing. I just tested it and it works as expected. Thank you so much for taking care of this in such a short timeframe.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants