Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

InvalidCastException in NumericUpDown #9848

Closed
nsrahmad opened this issue Jan 3, 2023 · 3 comments
Closed

InvalidCastException in NumericUpDown #9848

nsrahmad opened this issue Jan 3, 2023 · 3 comments
Labels

Comments

@nsrahmad
Copy link

nsrahmad commented Jan 3, 2023

I am learning so I don't know If I am doing something wrong or it is a bug.

Describe the bug
NumericUpDown shows System.InvalidCastException: Unable to convert object '89.6' of type 'System.Double' to type 'System.Nullable'1[System.Decimal]'.
I am using a class implementing IValueConverter to convert the value.

To Reproduce
Project has just a few lines changed from template:
https://github.com/nsrahmad/7GUI-Avalonia/tree/master/TemperatureConverter

Expected behavior
No errors. The error contains the right answer, so conversion is happening.

Screenshots
TempConv

Desktop (please complete the following information):

  • OS: Windows 10
  • Version 11.0.0-preview4

Additional context
Add any other context about the problem here.

@nsrahmad nsrahmad added the bug label Jan 3, 2023
@timunie
Copy link
Contributor

timunie commented Jan 3, 2023

@nsrahmad I think we had a breaking change here in 11.0. The reason is, that double has rounding issues where decimal doesn't have these problems. This is important if you work with percentage or currency values.

I see two options for you:

  1. use decimal in the bound Property
  2. use a Converter

See also: #5981

@maxkatz6
Copy link
Member

maxkatz6 commented Jan 3, 2023

Not related but you also have a typo here
image

@nsrahmad
Copy link
Author

nsrahmad commented Jan 4, 2023

@maxkatz6 Thanks.

@timunie Using decimal sort of worked. But I started to get StackOverflowException if I try to change the value with mouse wheel 3 or 4 times.

It took me some time to remove FormatString to uncover rounding issues in my converter. So trying to convert 1 celsius to fahrenheit would give:

> Console.WriteLine((1M * (9M / 5M)) + 32M);
33.8

But trying to convert back 33.8 to celsius would give:

> Console.WriteLine((33.8M - 32M) * (5M / 9M));
1.0000000000000000000000000001

I had to call decimal.Round on the result to fix it. Thank you for your help.

@nsrahmad nsrahmad closed this as completed Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants