Error rounding BigDecimal

4

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

I'm having difficulty converting, I've tried 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 this way:

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 the excerpt:

bd.setScate(2,BigDecimal.ROUND_UP);
  

ERROR The method setScate (int, int) is undefined for the type BigDecimal

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

    
asked by anonymous 17.11.2015 / 03:49

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 at runtime, for example.

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

Properly set up an IDE such as Elipse, NetBeans or IntelliJ and avoid silly errors.

Unchanging Objects

It was also noted by the author himself that he was not updating the variable bd with the rounded value.

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

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

So the correct one is:

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

As a result, we'll have:

numero: 3.14159
numeroArredondado: 3.15
    
17.11.2015 / 08:44