diff --git a/ClickHouse.Client.Tests/Numerics/ClickHouseDecimalTests.cs b/ClickHouse.Client.Tests/Numerics/ClickHouseDecimalTests.cs index 0bd203ce..eb1cc1e1 100644 --- a/ClickHouse.Client.Tests/Numerics/ClickHouseDecimalTests.cs +++ b/ClickHouse.Client.Tests/Numerics/ClickHouseDecimalTests.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.Linq; +using System.Numerics; using System.Threading.Tasks; using ClickHouse.Client.ADO; using ClickHouse.Client.Numerics; @@ -209,6 +210,7 @@ public void ShouldRoundtripIntoDouble(double @double) } [Test] + [TestCase(typeof(bool))] [TestCase(typeof(byte))] [TestCase(typeof(sbyte))] [TestCase(typeof(short))] @@ -220,11 +222,20 @@ public void ShouldRoundtripIntoDouble(double @double) [TestCase(typeof(float))] [TestCase(typeof(double))] [TestCase(typeof(decimal))] + [TestCase(typeof(string))] public void ShouldConvertToType(Type type) { - ClickHouseDecimal source = 5m; - var result = Convert.ChangeType(source, type); - Assert.AreEqual(source.ToString(), result.ToString()); + var expected = Convert.ChangeType(5m, type); + var actual = Convert.ChangeType(new ClickHouseDecimal(5m), type); + Assert.AreEqual(expected, actual); + } + + [Test] + public void ShouldConvertToBigInteger() + { + var expected = new BigInteger(123); + var actual = new ClickHouseDecimal(123.45m).ToType(typeof(BigInteger), CultureInfo.InvariantCulture); + Assert.AreEqual(expected, actual); } [Test] diff --git a/ClickHouse.Client/Numerics/ClickHouseDecimal.cs b/ClickHouse.Client/Numerics/ClickHouseDecimal.cs index e221f7b7..23ceed7d 100644 --- a/ClickHouse.Client/Numerics/ClickHouseDecimal.cs +++ b/ClickHouse.Client/Numerics/ClickHouseDecimal.cs @@ -401,9 +401,14 @@ public static ClickHouseDecimal Parse(string input, IFormatProvider provider) public object ToType(Type conversionType, IFormatProvider provider) { - if (conversionType == typeof(int)) - return ToInt32(provider); - throw new NotSupportedException(); + if (conversionType == typeof(BigInteger)) + { + var mantissa = this.Mantissa; + var scale = this.Scale; + Truncate(ref mantissa, ref scale, 0); + return mantissa; + } + return Convert.ChangeType(this, conversionType); } public int CompareTo(decimal other) => CompareTo((ClickHouseDecimal)other);