the code:
Float f = Float.parseFloat(\"1.80\");
System.out.println(f);
prints \"1.8\"
on screen.
I need to keep the 0 in the float
You are confusing a number value and its formatting. It is not possible to actually store 1.80
as a float, however it is possible to display the number as a formatted String
which forces two decimal places. Your options are:
Keep the original String that the user entered, if the number of decimal places they gave
matters
Store the number as a float
, but when displaying the number force it to display with two decimal places like this:
System.out.printf("%.2f\n", f);