Error when rounding Bigdecimal

Asked

Viewed 336 times

4

I’m performing a calculation with two methods, only since the attributes are of the type double, I’m using the BigDecimal to perform the operation and after that convert to two decimal places only.

I’m having trouble converting, I’ve tried it in two ways:

public double CalcularQntConsumido (){

    BigDecimal bd = new BigDecimal(consumo.getRegistro());
    BigDecimal bd2 = new BigDecimal(ds.getBuscarRegistro());
    BigDecimal bd3 = new BigDecimal(ds.getUltimoConsumo());


    if(ds.BuscarConsumo().size() == 0){

        qntConsumido =  bd.subtract(bd2).doubleValue();
        bd = new BigDecimal(qntConsumido);
        bd.setScate(2,BigDecimal.ROUND_UP);
        double valorFormatado = bd.doubleValue();
        return valorFormatado;


    }

    else{


        qntConsumido = bd.subtract(bd3).doubleValue();
        bd = new BigDecimal(qntConsumido);
        bd.setScate(2,BigDecimal.ROUND_UP);
        double valorFormatado = bd.doubleValue();
        return valorFormatado;

    }

}

And so:

public double CalcularQntConsumido (){

    if(ds.BuscarConsumo().size() == 0){

        qntConsumido = consumo.getRegistro() - ds.getBuscarRegistro();
        BigDecimal bd = new BigDecimal(qntConsumido);
        bd.setScate(2, BigDecimal.ROUND_UP);  
        double valorFormatado = bd.doubleValue();
        return valorFormatado;


    }

    else{

        qntConsumido = consumo.getRegistro() - ds.getUltimoConsumo() ;
        BigDecimal bd = new BigDecimal(qntConsumido);
        bd.setScate(2, BigDecimal.ROUND_UP);  
        double valorFormatado = bd.doubleValue();
        return qntConsumido;

    }

}

Both are returned error in stretch:

bd.setScate(2,BigDecimal.ROUND_UP);

ERROR The method setScate(int, int) is Undefined for the type Bigdecimal

I can’t convert to 2 decimal places, can anyone tell me how to do it?

1 answer

9


Method that does not exist

The error message "method ??? is Undefined" means that the method does not exist.

This could occur if you were using different versions of Java or some library at compile time and runtime, for example.

However, in this case, the method is called setScale (scale) and not setScate with t armadillo.

Correctly set up an IDE as Ellipse, Netbeans, or Intellij and avoid silly errors.

Immutable objects

It was also noticed by the author himself that he was not updating the variable bd rounded.

As described in the documentation, BigDecimal is immutable. The same is true for almost all basic Java types like String and Integer (and makes the class Calendar a black sheep).

This means that each method that transforms the value of a BigDecimal actually returns a new instance with the value transformed, while the original instance remains with the same value.

Therefore, the correct is:

BigDecimal numero = new BigDecimal("3.14159");
BigDecimal numeroArredondado = numero.setScale(2, BigDecimal.ROUND_UP);

As a result, we will have:

numero: 3.14159
numeroArredondado: 3.15
  • Really utluiz, I was wrong about the syntax, I did not know until then the Bigdecimal, this being new for me. I corrected as directed me, but even so it does not convert to the 2 decimal places only, it continues to bring a very extensive number, in both forms that I posted in the initial question, I left the second form as definitive. Could you tell me what I am doing wrong not to convert to the two decimal places only? I debugged the code and could not find the error. thank you

  • 1

    utluiz, I did, was not passing the variable again in this way bd = bd.setScale(2, Bigdecimal.ROUND_UP); corrected and worked. thanks

  • @Geovanirafaelsório Hum... good catch. This is another common problem with Bigdecimals.

Browser other questions tagged

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