6

我有两位小数:

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

我有一个可接受的误差范围;这是0.010
所以我需要的是一个检查方法:

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

我该怎么写?

4

1 回答 1

20
if (Math.Abs(first - second) <= margin)
于 2012-10-05T17:50:52.873 回答