开发者

How to compare two MyDouble values?

开发者 https://www.devze.com 2023-02-21 10:38 出处:网络
I want to开发者_开发技巧 compare two MyDouble values with zero. if(getA()>(MyDouble.zero)) //where getA() is MyDouble

I want to开发者_开发技巧 compare two MyDouble values with zero.

if(getA()>(MyDouble.zero)) //where getA() is MyDouble 

But it does not let me do that. Does anyone knows how to solve it?


You have to write a Comparator<MyDouble> that does the job. You'll implement the Comparator interface.

You won't be able to use the '>' comparison symbols to do it. You'll do something like this:

x.compareTo(y)


You should implement duffmo's solution as this is what Double and all Numbers do. Another way to solve this is to access the fields directly or provide a specific method to do the comparison. (This could be more efficient than implementing compareTo)

if(getA().value > MyDouble.ZERO.value) // Constants are in UPPER_CASE

or

if(getA().greaterThan(MyDouble.ZERO))
0

精彩评论

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

关注公众号