nowYouSeeMe
nowYouSeeMe

Reputation: 999

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))

Upvotes: 0

Views: 79

Answers (1)

Miguel Carreira
Miguel Carreira

Reputation: 526

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))

Upvotes: 1

Related Questions