I need to input number as a String from user and parse it to the Double object.
I'd like it to be available to take comma, as a decimal separator, and dot as well and save it and outuput only with a dot.
Example: 22,33-->22.33 and 22.33-->22.33
What I use is:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
try {
Number number = format.parse(formDto.getWeight());
kitten.setWeight(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}
But it only gets values with ',' separator. When user inputs with dot it loses all decimals and returns that 22.33 -->22.0 or 4.1 --> 4.0 When i debug i see that it's a parsing problem (obvious) but have no idea what is good practice to solve it.
Like Mohit Thakur said, but compilable.
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
try {
Number number = format.parse(formDto.getWeight().replace('.', ','));
kitten.setWeight(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}