How can I compare if BigDecimal
value is greater than zero?
-
1look into https://github.com/mortezaadi/bigdecimal-utils there is a method isPositive() there are also like is(bigdecimal).isZero(); is(bigdecimal).notZero(); is(bigdecimal).isPositive(); // greater than zero is(bigdecimal).isNegative(); // less than zero is(bigdecimal).isNonPositive(); // less than or equal zero is(bigdecimal).isNonNegative(); – Morteza Adi Sep 30 '18 at 11:47
-
@MortezaAdi since `BigDecimal` implements `Comparable` the comparing functions like `lt, le, eq, ne` better be moved to a `ComparableUtils`. So they can be used for any other classes like `Date` or custom types. – djmj Jun 26 '19 at 11:23
-
@djmj requirements defines implementation, there were no need nor intention to build ComparableUtils. Beside the functionality of lt, le, eq, etc are totally different than generic one. – Morteza Adi Jun 27 '19 at 12:09
9 Answers
It's as simple as:
if (value.compareTo(BigDecimal.ZERO) > 0)
The documentation for compareTo
actually specifies that it will return -1, 0 or 1, but the more general Comparable<T>.compareTo
method only guarantees less than zero, zero, or greater than zero for the appropriate three cases - so I typically just stick to that comparison.

- 1,421,763
- 867
- 9,128
- 9,194
-
59An extra word of warning is called for. Suppose that `value` has a value of zero but a non-zero scale (e.g. it evaluates to `0.00` instead of `0`). You probably want to consider that it's equal to zero. The `compareTo()` method will indeed do this. **But the `equals()` method will not.** (Another proof, if any were needed, that Loki or one of his avatars is alive and well and has moved into software development.) – Andrew Spencer Dec 06 '11 at 11:16
-
16While I agree that this is the idiomatic solution in Java, I don't think it's actually readable. Every time I encounter an expression like this, I find myself writing a test to reassure myself that I've got it the right way around. Perhaps the fact that recently added classes, like `LocalDate` include [`isBefore`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#isAfter-java.time.chrono.ChronoLocalDate-) is an indication that Oracle feel the same way. It's not ideal, but I think it's marginally more readable in these circumstances to write a utility `isGreaterThan` method. – Mark Slater Nov 14 '16 at 11:27
-
-
2@Angelina: I'd expect it to, certainly - those values *aren't* greater than 0, so I'd expect `compareTo` to return 0. But if you're worried, it should be easy for you to test. – Jon Skeet Mar 29 '18 at 13:40
-
@MarkSlater I agree, the compareTo is just Java should invest more time in improving readability and it makes no sense if everyone has to create its own readable methods in every project. In C# you can also create extension methods for existing classes so you can directly use `value.isGreaterThen(foo)`` – djmj Jun 26 '19 at 11:26
-
1
if (value.signum() > 0)
signum
returns -1, 0, or 1 as the value of this BigDecimal is negative, zero, or positive.

- 7,091
- 27
- 36

- 9,208
- 3
- 35
- 38
-
13BigDecimal.compareTo() starts by comparing signums as an optimisation. So it's probably best to call compareTo(), as it's more intention-revealing and only costs the price of an extra method call (which I suspect would get inlined anyway). – Andrew Spencer Dec 06 '11 at 10:54
-
34This is part of the public API. so it is, to me, a better way. the intention IS to determine if the sign is positive (ie > ZERO) – Marc Jan 24 '12 at 13:56
-
Do you think it's higher performance if it only grabs the first character of a `String` initialized `BigDecimal` to see if it's `-` or not? That's my best guess of how it could work. Seems faster to actually determine if the number's `0`. Is this correct? Thank you so very much in advance Anton Bessonov! – Jan 24 '14 at 19:26
-
@user1382306 No it's not. That way you need to convert the whole BigDecimal into string. The BigDecimal.signum() is either using Long.signum that uses bit shifting to see if the long has a signum or BigInteger.signum() that stores the signum value in an internal int field and simply return the value of that field. – Lakatos Gyula Jan 17 '17 at 13:12
-
8If you look at decompiled `BigDecimal.compareTo()` method, you will find that it calls `signum()` twice. So as far as performance goes, `signum()` is better. – MeIr Jun 05 '17 at 14:34
-
I used this answer and it worked fine at the beginning, but later it failed on me when I received a number with decimal cases. It is still a valid answer, of course, but one should be aware if you are expecting int numbers or decimals. – jfajunior Nov 12 '19 at 15:00
-
3
-
4@İsmailYavuz I'm sorry for the incomplete comment! I should at least write a proof of the arguments, my bad. Now I was looking for what happened, because I remember that I had to change things from "signum" to "compareTo" and I have no idea why. It was probably my mistake, as I did several tests now and I didn't find any problems with the "signum" using numbers with decimals. – jfajunior May 12 '20 at 07:41
it is safer to use the method compareTo()
BigDecimal a = new BigDecimal(10);
BigDecimal b = BigDecimal.ZERO;
System.out.println(" result ==> " + a.compareTo(b));
console print
result ==> 1
compareTo()
returns
- 1 if a is greater than b
- -1 if a is less than b
- 0 if a is equal to b
now for your problem you can use
if (value.compareTo(BigDecimal.ZERO) > 0)
or
if (value.compareTo(new BigDecimal(0)) > 0)
I hope it helped you.

- 6,079
- 5
- 39
- 55

- 820
- 7
- 12
Use compareTo()
function that's built into the class.

- 30,615
- 24
- 120
- 162

- 305,152
- 44
- 369
- 561
using ".intValue()" on BigDecimal object is not right when you want to check if its grater than zero. The only option left is ".compareTo()" method.

- 21
- 2
This works in Kotlin:
value > BigDecimal.ZERO

- 337
- 3
- 12
-
1`value` is a _BigDecimal_, and so is BigDecimal.ZERO. You cannot compare BigDecimals with >. – Jan B. Apr 13 '21 at 09:04
-
Perhaps,
BigDecimal a = new BigDecimal(10);
if(a.abs().equals(a)){
// positive
}else {
// negative
}
of course, the above doesn't count on the zero case.

- 2,548
- 9
- 44
- 74
Personally, I've used BigDecimal.ZERO to check that my value is greater than zero (0). And it works very well. I think this option is well suited. Or you can use signum(), which also works very well.
for (int i = 0; i < this.listTypes.size(); i++) {
if (this.listTypes.get(i).isAutorise()
&& this.listTypes.get(i).getPlafond() != null
&& this.listTypes.get(i).getPlafond().compareTo(BigDecimal.ZERO) > 0) {
this.listTypes.get(i).setProcuration(this.entity);
this.listTypes.get(i).setActeur(this.entity.getMandataire());
this.acteurTypeOperationService.addOne(this.listTypes.get(i));
}
}

- 1
- 1
BigDecimal obj = new BigDecimal("100");
if(obj.intValue()>0)
System.out.println("yes");

- 275
- 3
- 12
-
The obj holds numaric string, In the accepted answer the value is not declared, might be confusing. My answer will have more clarity. – Rama Krishna Feb 19 '18 at 11:43
-
it's for any value, no one will upvote based on input for this simple things, it may help for complex scenario. you are converting to intvalue again. so cost to convert, and this will fail for 0.33 as it will convert to int 0 – Satish Patro Aug 26 '20 at 14:35
-
@RamaKrishna, what if my obj = `BigDecimal("0.01")`? Would it print "yes"? – epox Aug 12 '22 at 12:14