开发者

Compare double till precision 2

开发者 https://www.devze.com 2022-12-27 12:14 出处:网络
I want to compare two double values till the second digit from decimal point. I am using following statement but I think it is not working properly.

I want to compare two double values till the second digit from decimal point. I am using following statement but I think it is not working properly.

dbl1.ToString("g2", CultureInfo.InvariantCulture).Equals( dbl2.ToString("g2", CultureInfo.InvariantCulture))

Am I missing anything 开发者_JS百科here?


You could use Math.Abs(dbl1 - dbl2) < 0.01. This will only consider the first two digits after the decimal point.


dbl1.ToString("g2", CultureInfo.InvariantCulture).Equals( dbl2.ToString("g2", CultureInfo.InvariantCulture))

is working for me. I was just looking for any other alternative.

0

精彩评论

暂无评论...
验证码 换一张
取 消