Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# round and SQL round functions yields different outputs?

I am using ROUND function from C# and SQL, and surprisingly both are yielding different results.

In SQL: ROUND(1250.00, -2) = 1300

In C# ROUND 1250 with round and precision = 2 = 1200

Has anyone came across this situation before?

like image 226
Vijay Balkawade Avatar asked Feb 02 '23 11:02

Vijay Balkawade


1 Answers

C# uses banker's rounding by default, where when you're exactly on the .5 mark, it rounds to the nearest even number instead of always rounding up.

The remarks section of the msdn article describes this behavior. Basically it's to reduce rounding errors when you accumulate a lot of rounded numbers together.

like image 132
Tanzelax Avatar answered Feb 05 '23 00:02

Tanzelax