DarkWanderer / ClickHouse.Client

.NET client for ClickHouse
MIT License
302 stars 58 forks source link

Incorrect conversion of ClickHouseDecimal to intger types #459

Closed MaceWindu closed 2 months ago

MaceWindu commented 2 months ago

Version: latest (7.2.1)

// works property for decimal with 0 scale
var d1 = new ClickHouseDecimal(1m);
Console.WriteLine("{0}", (decimal)d1); // 1
Console.WriteLine("{0}", (int)d1); // 1

// works incorrectly for decimal with non-0 scale
d1 = new ClickHouseDecimal(1.0m);
Console.WriteLine("{0}", (decimal)d1); // 1.0
Console.WriteLine("{0}", (int)d1); // 100 - wrong!
Console.WriteLine("{0}", (int)(decimal)d1); // 1 (workaround using intermediate decimal representation)

d1 = new ClickHouseDecimal(1.1m);
Console.WriteLine("{0}", (decimal)d1); // 1.1
Console.WriteLine("{0}", (int)d1); // 110 - wrong!
Console.WriteLine("{0}", (int)(decimal)d1); // 1 (workaround using intermediate decimal representation)
MaceWindu commented 2 months ago

Issue affects all integral types with conversion operator: int/uint/long/ulong

DarkWanderer commented 2 months ago

Thank you for reporting this - that's a bit embarassing, as it should have been covered by tests in theory. Fixed in release 7.2.2