Hacker News new | ask | show | jobs
by rednb 25 days ago
Wow, what a vicious footgun. I do a lot of work related to pricing, invoicing, exchange rates etc... where decimals do matter, so this kind of stresses me out. Thanks for the heads up.
3 comments

You shouldn’t cast from double to decimal anyway. Just use decimal throughout.
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.

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.
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.
In cases where you have to convert from float/double to decimal you should use an explicit rounding method, not just a raw cast.
The code (decimal)0.1 shouldn't involve a double in the first place.
The literal 0.1 is a double. If you want a decimal literal use the “m” postfix like 0.1m.

Then you dont need the cast and there is no problem with hidden roundings.

> The literal 0.1 is a double.

I know. It shouldn't be.

Why? What do you mean?
Even among floats, why double in particular? Why not single? Why not quad?

If practical, I'd give it either infinite precision or the type of whatever variable it's being stored into.

If that doesn't work and it has to be a specific type in all cases, making it a special decimal that converts to double without a warning would be a better baseline.

> don't cast from double to decimal

I hope this is a compiler warning at the least.

It is an error if you dont explicitly cast: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
A cast is an explicit syntax which let the programmer say “I know more than the compiler”. If you don’t, don’t use cast.

If the conversion is safe (eg converting from an integer to decimal) then the cast syntax is unnecessary.

0.1 is not representable in binary floating point. The old behavior was adding magic that shouldn't be there.
Did they deprecate it, then?
> Wow, what a vicious footgun.

We do too at $dayjob, but use cents for everything and at the end of the calculation we divide by 100 to get the dollar amount.

Exchange rates are 4 decimals, so you would still not get bit by this.

Thanks for sharing, this is an interesting approach. Unfortunately, too late for us to transition to this across the board, but I'll definitely keep it in mind moving forward.