0

have faced below problem while writing query, not sure why it is happening Please find the below example

SELECT 21000.000000/22000.000000 * 22000.000000

sql engine result is 20999.999999999999990000000

But if you do the same through calculator, we will be getting 21000. Why is it like this? and how to achieve the calculator result with out rounding off

  • a good pocket calculator has additional places of accuracy that are not displayed, and this can avoid the user seeing these numeric round-off discrepancies. Some older or cheap calculators still regular display these anomalies though, sometimes simply by pressing '1 divide 3 equals multiply 3 equals'. The problem for fixed decimal places is that 21/22 is an infinitely recurring decimal, and the decimal that is held by a computer has to be truncated. When you multiply it back, the missing part becomes apparent. – Cato Jan 25 '19 at 17:48

1 Answers1

0

You could use:

SELECT CAST(21000.000000/22000.000000 * 22000.000000 AS NUMERIC)

Or

SELECT CAST(21000.000000/22000.000000 * 22000.000000 AS FLOAT)

seantunwin
  • 1,698
  • 15
  • 15