Java: Check if two double values match on specific no of decimal places

前端 未结 5 1131
旧巷少年郎
旧巷少年郎 2020-12-19 02:42

Comparing those two values shall result in a \"true\":

53.9173333333333  53.9173
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 02:53

    Apache commons has this: org.apache.commons.math3.util.Precision equals(double x, double y, double eps)

    epsilon would be the distance you would allow. Looks like yours would be 1e-5?

    The source-code of this method looks like it uses Math.abs as suggested in other answers.

提交回复
热议问题