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?