Skip to content
Advertisement

Dividing by decimal values – rounding issue

I’m trying to divide two decimal values, in hope they will return the following; 1.132930.

However, SQL appears to be rounding the value to 1.132931.

The value returned before conversion is 1.1329305135951.

The SQL code i’m using is as follows:

DECLARE @p1 DECIMAL(10,2)
DECLARE @p2 DECIMAL(10,2)

SET @p1 = 225.00
SET @p2 = 198.60

SELECT Cast(@p1/@p2 AS DECIMAL(9,6))

Advertisement

Answer

You can round the value before the cast as following:

DECLARE @p1 DECIMAL(10,2)
DECLARE @p2 DECIMAL(10,2)

SET @p1 = 225.00
SET @p2 = 198.60

SELECT Cast(ROUND(@p1/@p2,6,1) AS DECIMAL(9,6))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement