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?
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
– Geovani Rafael Sório
utluiz, I did, was not passing the variable again in this way bd = bd.setScale(2, Bigdecimal.ROUND_UP); corrected and worked. thanks
– Geovani Rafael Sório
@Geovanirafaelsório Hum... good catch. This is another common problem with Bigdecimals.
– utluiz