This is strange behaviour that I haven t noticed before. I expected not to be able to cast from a double
but instead the double
value is implicitly converted to an int
and I silently lose the fractional portion. Can I really not convert from int
s without also converting from double
s?
readonly struct TestDecimal {
public decimal Value { get; }
TestDecimal(decimal value) =>
Value = value;
public static explicit operator TestDecimal(int x) => new(x);
}
...
// these assertions fail, but really this shouldn t even compile
[TestMethod]
public void Test_CastFromDouble() {
var value = (TestDecimal)12.34; // shouldn t compile
Assert.AreEqual(12.34m, value.Value); // assertion failed, actual value: 12.0m
}
[TestMethod]
public void Test_CastFromDecimal() {
var value = (TestDecimal)12.34m; // shouldn t compile
Assert.AreEqual(12.34m, value.Value); // assertion failed, actual value: 12.0m
}