-2

I suppose that create table query with column decimal(12,2) means number with maximum 12 digits and 2 of that are decimal, right? When I use update statement on this column, like

"update table set 'x' = 'x' + 0.01 where.....""

I always get a number with 12 digits behind decimal . like, 0 = 0 + 0.01 result is 0.010000000001

which is not big problem for me but that 1 digit on the end is. Should I change my create table statement cause obviously that 12 number doesn't work right thing for me, thank you

daneg
  • 29
  • 4
  • Do you obtain that result in the CLI or through the C API? My guess is that you use the second one and `sqlite3_column_double`. Please edit your question to clarify it. – Piotr P. Karwasz Dec 25 '20 at 23:31
  • I am using c# to send update statement. My column in sqlite is decimal – daneg Dec 26 '20 at 00:36
  • 1
    Start by giving https://sqlite.org/datatype3.html a good read. – Shawn Dec 26 '20 at 03:46
  • 1
    And [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Shawn Dec 26 '20 at 03:47

1 Answers1

0

The method you use to retrieve the column clearly retrieves the column as double. Since there is no floating-point number corresponding to 0.01, the value you obtain is rounded.

While retrieving the column be sure to use a class like Decimal, that performs operation on decimal numbers with the correct rounding and retrieve your column as:

Round(dataReader.getDecimal(), 2);

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43