Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to decimal number with 2 decimal places in Java

In Java, I am trying to parse a string of format "###.##" to a float. The string should always have 2 decimal places.

Even if the String has value 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);

It prints:

string liters of petrol putting in preferences is 010.00 
liters of petrol before putting in editor: 10.0
like image 249
adityag Avatar asked Jul 04 '13 07:07

adityag


People also ask

How do you correct to 2 decimal places in Java?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

How do I format a string to two decimal places?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

What is .2f in Java?

we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%. 3f", x would have been printed rounded to 3 decimal places.


3 Answers

Java convert 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)));

This prints:

8.88E-8
0.0000001
0.000000089
0.00000008880000000000000106383001366
0.00
like image 99
Eric Leschinski Avatar answered Sep 21 '22 03:09

Eric Leschinski


Use BigDecimal:

new BigDecimal(theInputString);

It retains all decimal digits. And you are sure of the exact representation since it uses decimal base, not binary base, to store the precision/scale/etc.

And it is not subject to precision loss like float or double are, unless you explicitly ask it to.

like image 25
fge Avatar answered Sep 23 '22 03:09

fge


This line is your problem:

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

There you formatted your float to string as you wanted, but but then that string got transformed again to a float, and then what you printed in stdout was your float that got a 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 the existence of double and float as others suggested and just use BigDecimal object, it will save you a lot of headache.

like image 39
David Hofmann Avatar answered Sep 24 '22 03:09

David Hofmann