0

In my project, there is a function that gets the query results depending on the column type. In this case, the column type is defined in the Oracle database as: NUMBER(15, 5). But the actual query result is an integer number such as : 2000 But the problem is, that in the code, we convert it to decimal and the result will be changed to: 2000.00000

We use BigDecimal, and we put the scale on the result. Is there a short, correct way to deal with all results from database within the column type of: NUMBER(15, 5) ? When the result is Integer is will be Integer, and when it's a decimal with scale of 5 then the result will be like this. This is the case within our switch case that deals with it:

                    String cellData;
                    case Types.DECIMAL:
                        BigDecimal number = rs.getBigDecimal(i);
                        if (number != null) {
                            int scale = rsMetadata.getScale(i);
                            int precision = rsMetadata.getPrecision(i);
                            cellData = number.setScale(scale).toPlainString();

Maybe something to do with the precision? Because now we only use the scale.

MT0
  • 143,790
  • 11
  • 59
  • 117
elads11
  • 337
  • 1
  • 8
  • 2
    `BigDecimal` is analogous to an arbitrary bitlength `double`, so the decimal place being printed would be expected. If you want the integer form of that number, consider using either `BigInteger` or using a proper scale and precision for your `NUMBER` datatype. – Rogue Jul 11 '22 at 13:53
  • That's is the thing, I want the data returned from database to be handled accordingly, when it's decimal with 5 numbers after the dot, do it as it is now with bigDecimal, but when the result is integer, keep it that way – elads11 Jul 11 '22 at 13:57
  • Yes, Added this line as a condition and it works: number.stripTrailingZeros().scale() <= 0 – elads11 Jul 11 '22 at 14:28

1 Answers1

1

You could do something like this to output either an integer or a decimal with the precision you need:

String cellData;
BigDecimal number = rs.getBigDecimal(i);
BigDecimal integerValue = null;
if (number != null) {
    int scale = rsMetadata.getScale(i);
    int precision = rsMetadata.getPrecision(i);
    number.setScale(scale, BigDecimal.ROUND_HALF_UP);
    // something else?
    if (Types.INTEGER) {
        integerValue = number.toBigInteger(); // or number.toBigIntegerExact();
    }
}
// store decimal or integer
hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • Is there a reason why the line number.setScale(scale, BigDecimal.ROUND_HALF_UP); gets deprecated for me? – elads11 Jul 11 '22 at 14:36
  • 1
    It was deprecated somewhere around Java 9-11, you should use `RoundingMode` instead ([per the javadoc](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html#setScale(int,int))) – Rogue Jul 11 '22 at 14:53
  • @elads11, sorry, I was in a hurry. I forgot to mentioned what Rogue posted on the comments above. – hfontanez Jul 12 '22 at 00:03