Convert string to decimal with two decimal places in Java

In Java, I am trying to parse a format string "###.##" into a float. The string must always contain 2 decimal places.

Even if String is 123.00 , the float should also be 123.00 , not 123.0 .

This is what I have so far:

 System.out.println("string liters of petrol putting in preferences is " + stringLitersOfPetrol); Float litersOfPetrol = Float.parseFloat(stringLitersOfPetrol); DecimalFormat df = new DecimalFormat("0.00"); df.setMaximumFractionDigits(2); litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol)); System.out.println("liters of petrol before putting in editor: " + litersOfPetrol); 

He prints:

 string liters of petrol putting in preferences is 010.00 liters of petrol before putting in editor: 10.0 
+3
source share
6 answers

This line is your problem:

 litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol)); 

There you formatted your float to a string as you wanted, but then that string was transformed into a float again, and then what you printed in stdout was your float, which got standard formatting. Take a look at this code

 import java.text.DecimalFormat; String stringLitersOfPetrol = "123.00"; System.out.println("string liters of petrol putting in preferences is "+stringLitersOfPetrol); Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol); DecimalFormat df = new DecimalFormat("0.00"); df.setMaximumFractionDigits(2); stringLitersOfPetrol = df.format(litersOfPetrol); System.out.println("liters of petrol before putting in editor : "+stringLitersOfPetrol); 

And by the way, when you want to use decimals, forget about the existence of double and float, as others have suggested, and just use the BigDecimal object, this will save you a lot of headache.

+7
source

Java converts a string to decimal:

 String dennis = "0.00000008880000"; double f = Double.parseDouble(dennis); System.out.println(f); System.out.println(String.format("%.7f", f)); System.out.println(String.format("%.9f", new BigDecimal(f))); System.out.println(String.format("%.35f", new BigDecimal(f))); System.out.println(String.format("%.2f", new BigDecimal(f))); 

Fingerprints:

 8.88E-8 0.0000001 0.000000089 0.00000008880000000000000106383001366 0.00 
+7
source

Use BigDecimal :

 new BigDecimal(theInputString); 

It saves all decimal digits. And you are sure of the exact representation, since it uses a decimal base, not a binary base, to maintain accuracy / scale / etc.

And this is not subject to precision loss, for example, float or double , unless you explicitly set it.

+4
source
 litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol)); System.out.println("liters of petrol before putting in editor : "+litersOfPetrol); 

You are printing Float here , which has no format.

To print a formatted float, simply use

 String formatted = df.format(litersOfPetrol); System.out.println("liters of petrol before putting in editor : " + formatted); 
+2
source

I just want to make sure that after converting this string, the floating point number will also have 2 decimal places.

You cannot, because floating point numbers do not have decimals. They have binary places that are not proportional to decimal places.

If you want decimal places, use decimal radius.

+2
source

Float.parseFloat () is a problem as it returns a new float .

Returns a new float, initialized with the value represented by the specified string, as performed by the valueOf method of the Float class.

You format for display only. This does not mean that the float will be represented by the same format inside.

You can use java.lang.BigDecimal .

I'm not sure why you use parseFloat() twice. If you want to display the float in a specific format, just format it and show it.

 Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol); DecimalFormat df = new DecimalFormat("0.00"); df.setMaximumFractionDigits(2); System.out.println("liters of petrol before putting in editor"+df.format(litersOfPetrol)); 
+1
source

All Articles