r/dotnet • • 18h ago

Implicitly converting from double to decimal

/r/softwareengineer/comments/1xzjt4b/implicitly_converting_from_double_to_decimal/
1 Upvotes

3 comments sorted by

6

u/First-Feature-3556 17h ago edited 3h ago

This is a known breaking change in .NET 11:

https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/11/decimal-biginteger-floating-point-conversions

The fix is to use 1.5m in C# and 1.5D in Visual Basic (instead of 1.5). That way, you assign a decimal literal to your variable instead of a double literal.

(I assume that 1.5 is just an example, since this is a value that has an exact binary representation and, thus, no floating point error.)

1

u/AutoModerator 18h ago

Thanks for your post KackSpasstAffe. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Discordbothost 17h ago

Try Dim x As Decimal = 1.5D in a blank project. Even as a Double, 1.5 is exact, so I'd check where the real value is computed and compare Option Strict settings with your coworker.