0

i have a question about bigdecimal compare. currently, i have two values ,they have different source, the first one is 748.30 which query from database, i use the resultSet.getBigDecimal(columnValue), then i get 748.30, and the second value is query by Spring-data-jpa which from another database, the value is 748.3000, when i use the method BigDecimal.compare to compare these two values, and i got the result is not 0, i am very confused, can you help me?

  • Can you include your actual comparison code? – Tim Biegeleisen Nov 01 '17 at 02:03
  • What is the value displayed when you use SQL to query the 2 different data? – Alex Nov 01 '17 at 02:04
  • 2
    BigDecimal does not have a method `compare`. It does have a method `compareTo`. When you run `System.out.println(new BigDecimal("748.30").compareTo(new BigDecimal("748.3000")));`, the output is 0 (**zero**), unlike your question says. So there is something else going on in your application. – Erwin Bolwidt Nov 01 '17 at 02:07
  • 2
    A useful read is [BigDecimal equals() versus compareTo()](https://stackoverflow.com/questions/6787142/bigdecimal-equals-versus-compareto) – Erwin Bolwidt Nov 01 '17 at 02:09
  • Hi @TimBiegeleisen, i have a class named tradeRecord, i want to compare the balnace, the code is `tradeRecord.setBalance(resultSet.getBigDecimal("surplus_money"));`, and the other class is cardinfo, the balance property of cardinfo is also a bigDecimal, and the scale is 4, the value is setted by spring-data-jpa, the compare code is 'lastAskbalance.add(tradeRecord.getTradeAmount().multiply(inOutValue)).compareTo(tradeRecord.getBalance()) != 0' – maoningchuan Nov 01 '17 at 02:33
  • Hi @ErwinBolwidt, yes , i tried this , get the result as you , so i am very confused. – maoningchuan Nov 01 '17 at 02:39
  • @maoningchuan: Don't put your code and explanation in a comment, put it in your question. Edit your question and add the code there. Also make the text a little bit more readable by formatting it a little better (use paragraphs, etc.). – Rudy Velthuis Nov 01 '17 at 20:15

0 Answers0