2
Setting:
public void metodoX(Double valor) {
DecimalFormat df = new DecimalFormat("0.00");
String valorRound = df.format(valor);
Double valorRound2 = Double.parseDouble(valorRound);
...
}
Error:
Caused by: java.lang.NumberFormatException: Invalid double: "3,67"
Line with error:
Double valorRound2 = Double.parseDouble(valorRound);
Details:
The app is rodando perfeitamente no emulador API 23
, but running in the celular API 23, causa esse erro
.
Why only on the mobile gives the error ?
I’ve tried to:
Double valorRound2 = Double.valueOf(valorRound);
He’s expecting a figure with
.
for decimal separator, not comma. I don’t know why it produces this comma because apparently the decimal format should be with.
; one of the probable causes is that the emulator is with English locale and the Portuguese device– Jefferson Quesado
@Jeffersonquesado understood, it can be locale... you think with the bigdecimal, would solve ?
– rbz
If you’re not in scientific applications, I don’t even know why to use
double
for starters. I would always useBigDecimal
. You have a very interesting question on the subject, https://answall.com/q/219211/64969– Jefferson Quesado
@Jeffersonquesado So basically better use the
BigDecimal
for all cases that do not need to be floating (I believe 99% do not need more than 4 homes after the comma) !? Taking advantage, Bigdecimal already contains house limitation ?– rbz
Using Bigdecimal will not solve the
NumberFormatException
.– ramaral
The accuracy is arbitrary. I work with 30 to guarantee me in the calculation of taxes (even the user informing at most 2 decimal places of value, 4 houses of what we have stored in the table of taxes and 4 houses in the discount of the sale of the product). I believe your case is one of arbitrary accuracy. This will not avoid formatting issues, as @ramaral pointed out.
– Jefferson Quesado