Bigdecimal comparison returns unexpected result

Asked

Viewed 1,291 times

3

In one of my methods I make the following comparison, as in the example below:

public static void main(String[] args) {

    BigDecimal valor = new BigDecimal("100");
    exemplo(valor);

}

public static void exemplo(BigDecimal valor) {
    if (valor.compareTo(new BigDecimal("50.000")) == 1 || valor.compareTo(new BigDecimal("50.000")) == 0) {
        System.out.println("número maior");
    }
}

Passing as parameter the value "100", the problem is that the class Bigdecimal ends up considering the value "100" higher than the value "50.000", I tried to pass the value as: "50.000.00" to try to solve the problem, but the class does not allow me to do this, some solution?

  • 1

    But 100 is greater than 50. Could clarify better what you intend?

  • putting as an example our money, I am comparing 100 real with 50,000 thousand, there is no way to be bigger..

  • 1

    No, you’re comparing $100 to $50,000.

  • @bigown like so, I’m confused now I’m doing something wrong?

  • @Emanoel I tried to clarify the confusion in the answer. Take a look.

2 answers

5

The point is the decimal separator, so you’re comparing 100 to 50 (50,000 read "in English" is 50,000, or simply 50). Do not use dots to separate thousands. Actually this does not exist in programming. See:

public static void exemplo(BigDecimal valor) {
    if (valor.compareTo(new BigDecimal("50000")) == 1 || valor.compareTo(new BigDecimal("50000")) == 0) System.out.println("número maior");
}

Works perfectly.

The reason for not accepting 50.000.00 is because it has two decimal separators, or for what we use here in Brazil, it’s like it has two commas, which a number cannot have.

I imagine in your real problem is getting a value like string and it has decimal places. Then you must convert the value to BigDecimal according to the standard you receive. It’s something else entirely.

public static void exemplo(BigDecimal valor) {
    try {
        DecimalFormat df = new DecimalFormat("###,##0", new DecimalFormatSymbols (new Locale ("pt", "BR")));
        df.setParseBigDecimal(true);
        BigDecimal decimal = (BigDecimal)df.parse("50.000");
        if (valor.compareTo(decimal) == 1 || valor.compareTo(decimal) == 0) System.out.println("número maior");
        else System.out.println("número menor");
    } catch (ParseException e) { //só para facilitar, não faça isto
        e.printStackTrace();
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • It even exists, but not with a semicolon. Java has the _ and C++ has the '.

  • Thanks @bigown

  • @Emanoel edited to put something complementary that maybe you want.

4


You’re actually comparing 100 to 50. In English, the dot separates the entire part of the fractional part of a number from the comma. As indicated in official documentation, the BigDecimal accepts fractional numbers represented this way in your constructor. That’s what you’re doing without seeing.

To do what you want, compare it to "50,000".

  • Thanks @Pablo Almeida

Browser other questions tagged

You are not signed in. Login or sign up in order to post.