user3486647
user3486647

Reputation: 311

How to round numbers properly in SQL Server?

Below picture example, is there a way to make Total Cost v1 precisely equal to v2 in SQL Server?

I have a simple sales report.

My formula of Total Cost v1 in SQL Server is:

SUM(ROUND(([Cost]*(CAST([Count] as INT))), 2)) as 'Total Cost v1'

Is there a way to modify this above formula to let the final total match the v2? Otherwise if the list becomes big, bigger difference on the numbers.

The data type for Count is decimal(10, 4). The datatype for Cost is money.

enter image description here

Upvotes: 1

Views: 161

Answers (1)

DhruvJoshi
DhruvJoshi

Reputation: 17126

You should change your formula to

ROUND(SUM([Cost]*(CAST([Count] as INT))), 2) as 'Total Cost v1'

Upvotes: 1

Related Questions