News for hackers. Stuff that matters.
Re:What's new in the .NET 11 runtime by bazoom42 on Tuesday November 10, 2026 @02:41PM (#50445268)
You shouldn’t cast from double to decimal anyway. Just use decimal throughout.
Re:What's new in the .NET 11 runtime by snkline on Tuesday November 10, 2026 @02:55PM (#50445457)
Yeah, while I do agree this change in behavior could be a major footgun for a few people, the vast majority of use cases for decimal should be unaffected. Decimal is usually used for business applications to the exclusion of double/float, not alongside them (the only doubles I've seen in my company's applications are just storing Lat/Long values for addresses, everything else uses decimal values).

Hell, on the rare occasion I've needed to use a decimal literal in code, I just use a decimal literal: don't do (decimal)0.1, just use 0.1m.

Re:What's new in the .NET 11 runtime by FearNotDaniel on Tuesday November 10, 2026 @03:01PM (#50445521)
Exactly. If you want an actual decimal literal you should always write it as 1.2345m rather than converting it with the (decimal) prefix. The compiler refuses to accept a plain 1.2345 assigned to a decimal type so it’s not such a footgun as it may appear to some; though it might offer a better DX if the type of the literal was inferred from the thing you are trying to assign it to, that would no doubt have unintended consequences.
Re:What's new in the .NET 11 runtime by rednb on Wednesday November 11, 2026 @01:11AM (#50450216)
Fair enough, this is mostly what i do but since the runtime has been forgiving, i'll need to triple check that. I label all pure money fields as decimal, but am less confident for the ProductQuantity kind of fields. I typically use int for whole numbers but wouldn't be surprised if i used float occasionnally for quantities accepting decimals.
Re:What's new in the .NET 11 runtime by bazoom42 on Wednesday November 11, 2026 @03:44AM (#50451060)
In cases where you have to convert from float/double to decimal you should use an explicit rounding method, not just a raw cast.
Re:What's new in the .NET 11 runtime by mycall on Wednesday November 11, 2026 @12:57PM (#50456350)
> don't cast from double to decimal

I hope this is a compiler warning at the least.

Re:What's new in the .NET 11 runtime by tomalbrc on Wednesday November 11, 2026 @01:51PM (#50456995)
It is an error if you dont explicitly cast: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
Re:What's new in the .NET 11 runtime by bazoom42 on Thursday November 12, 2026 @05:14AM (#50465262)
A cast is the programmer saying “i know more than the compiler”. If you dont, dont use cast.
Re:What's new in the .NET 11 runtime by Dylan16807 on Wednesday November 11, 2026 @10:31PM (#50462723)
The code (decimal)0.1 shouldn't involve a double in the first place.
Re:What's new in the .NET 11 runtime by bazoom42 on Thursday November 12, 2026 @05:11AM (#50465241)
The literal 0.1 is a double. If you want a decimal literal use the “m” postfix like 0.1m.
Re:What's new in the .NET 11 runtime by Dylan16807 on Thursday November 12, 2026 @01:13PM (#50471603)
> The literal 0.1 is a double.

I know. It shouldn't be.