Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the equality of two decimals with an acceptable error margin

I have two decimals:

var first = 1.567m;
var second = 1.568m;
var areEqual = first == second; // false

And I have an acceptable margin of error; which is 0.010.
So what I need is a Check method:

Check(first: 1.567m, second: 1.577m, margin: 0.010m); // true
Check(first: 1.567m, second: 1.578m, margin: 0.010m); // false
Check(first: 1.567m, second: 1.578m, margin: 0.011m); // true

How can I write that?

like image 340
Şafak Gür Avatar asked Oct 05 '12 17:10

Şafak Gür


1 Answers

if (Math.Abs(first - second) <= margin)
like image 126
SLaks Avatar answered Oct 28 '22 19:10

SLaks