private float parse(float val){       
     DecimalFormat twoDForm = new DecimalFormat("#.##");
     return Float.valueOf(twoDForm.format(val));
}

As long as you call it passing an valid float, your result will be a float. But you can't show the right most zero if its not a String.

Answer from Matheus on Stack Overflow
🌐
Quora
quora.com › How-do-you-convert-a-decimal-to-a-float-in-Java
How to convert a decimal to a float in Java - Quora
Answer: In Java, you can convert a decimal number to a float by using the [code ]floatValue()[/code] method or by casting the decimal number as a float. Here is an example of using the [code ]floatValue()[/code] method: [code]Copy codedouble decimalNum = 3.14; float floatNum = (float)decimalNum...
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
* * @author Javin Paul */ public ... From Java 5, String has a format() method String str = String.format("%.02f", pi); System.out.println("formatted float up to 2 decimals " + str); // If you just want to display, you can combine ...
🌐
Delft Stack
delftstack.com › home › howto › java › convert int into float java
How to Convert Int to Float in Java | Delft Stack
February 2, 2024 - Subsequently, the program uses System.out.println() to display the value of f. In this case, it will print 56.0 since float can accurately represent integer values without any loss of precision. ... The above code serves as a clear example of how Java handles implicit type casting, making it convenient for developers to work with different data types in their programs.
Find elsewhere
🌐
Quora
quora.com › How-do-I-round-off-a-float-to-2-decimal-points-in-Java
How to round off a float to 2 decimal points in Java - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Baeldung
baeldung.com › home › java › java numbers › how to round a number to n decimal places in java
How to Round a Number to N Decimal Places in Java | Baeldung
April 1, 2026 - These unexpected results occur because primitive types like float and double use binary representations, which can’t exactly represent some decimal values. As a result, rounding with these types can lead to subtle truncation or rounding errors. For example, the value 260.775 cannot be exactly represented as a double. Internally, it might be stored as slightly less than 260.775, so rounding it to two decimal places results in 260.77 instead of 260.78.
🌐
Attacomsian
attacomsian.com › blog › java-round-double-float
Round a double or float number to 2 decimal places in Java
November 26, 2022 - The DecimalFormat class can be used to round a double or floating point value to 2 decimal places, as shown below: double price = 19.5475; DecimalFormat df = new DecimalFormat("0.00"); System.out.println("Price: " + df.format(price)); // Price: ...
Top answer
1 of 7
177

I was working with statistics in Java 2 years ago and I still got the codes of a function that allows you to round a number to the number of decimals that you want. Now you need two, but maybe you would like to try with 3 to compare results, and this function gives you this freedom.

/**
* Round to certain number of decimals
* 
* @param d
* @param decimalPlace
* @return
*/
public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

You need to decide if you want to round up or down. In my sample code I am rounding up.

Hope it helps.

EDIT

If you want to preserve the number of decimals when they are zero (I guess it is just for displaying to the user) you just have to change the function type from float to BigDecimal, like this:

public static BigDecimal round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);       
    return bd;
}

And then call the function this way:

float x = 2.3f;
BigDecimal result;
result=round(x,2);
System.out.println(result);

This will print:

2.30
2 of 7
58

Let's test 3 methods:
1)

public static double round1(double value, int scale) {
    return Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale);
}

2)

public static float round2(float number, int scale) {
    int pow = 10;
    for (int i = 1; i < scale; i++)
        pow *= 10;
    float tmp = number * pow;
    return ( (float) ( (int) ((tmp - (int) tmp) >= 0.5f ? tmp + 1 : tmp) ) ) / pow;
}

3)

public static float round3(float d, int decimalPlace) {
    return BigDecimal.valueOf(d).setScale(decimalPlace, BigDecimal.ROUND_HALF_UP).floatValue();
}



Number is 0.23453f
We'll test 100,000 iterations each method.

Results:
Time 1 - 18 ms
Time 2 - 1 ms
Time 3 - 378 ms


Tested on laptop
Intel i3-3310M CPU 2.4GHz

Top answer
1 of 9
799

Using Math.round() will round the float to the nearest integer.

2 of 9
210

Actually, there are different ways to downcast float to int, depending on the result you want to achieve: (for int i, float f)

  • round (the closest integer to given float)

    i = Math.round(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -3
    

    note: this is, by contract, equal to (int) Math.floor(f + 0.5f)

  • truncate (i.e. drop everything after the decimal dot)

    i = (int) f;
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
  • ceil/floor (an integer always bigger/smaller than a given value if it has any fractional part)

    i = (int) Math.ceil(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  3 ; f =  2.68 -> i =  3
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -2 ; f = -2.68 -> i = -2
    
    i = (int) Math.floor(f);
      f =  2.0 -> i =  2 ; f =  2.22 -> i =  2 ; f =  2.68 -> i =  2
      f = -2.0 -> i = -2 ; f = -2.22 -> i = -3 ; f = -2.68 -> i = -3
    

For rounding positive values, you can also just use (int)(f + 0.5), which works exactly as Math.Round in those cases (as per doc).

You can also use Math.rint(f) to do the rounding to the nearest even integer; it's arguably useful if you expect to deal with a lot of floats with fractional part strictly equal to .5 (note the possible IEEE rounding issues), and want to keep the average of the set in place; you'll introduce another bias, where even numbers will be more common than odd, though.

See

http://mindprod.com/jgloss/round.html

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

for more information and some examples.

🌐
DaniWeb
daniweb.com › programming › software-development › threads › 32513 › how-can-i-get-float-value-with-2-decimal-point
java - How can I get float value with 2 decimal ... | DaniWeb
September 30, 2011 - For display only, build on jwenting’s point and fix both min and max fraction digits so you always see two decimals (server_crash’s "###.##" can drop trailing zeros): import java.text.NumberFormat; import java.util.Locale; double num = 1001.27124; NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); String out = nf.format(num); // "1001.27" System.out.println(out);
🌐
Delft Stack
delftstack.com › home › howto › java › how to print a float with 2 decimal places in java
How to Print a Float With 2 Decimal Places in Java | Delft Stack
February 2, 2024 - public class SimpleTesting { public static void main(String args[]) { float Pi = 3.1415f; System.out.println(Pi); // Get only 2 decimal points System.out.printf("%.2f", Pi); } } ... The DecimalFormat is a Java class that provides utility methods ...
🌐
Coderanch
coderanch.com › t › 383909 › java › convert-float-decimal-places
Is it possible to convert a float to two decimal places (Java in General forum at Coderanch)
October 29, 2007 - Can anyone help me to convert a float to two decimal places float f=16; System.out.println(f); It will print 16.0 But I want to get printed as 16.00 ... Welcome to JavaRanch. Something like the following should do the trick: ... Thanks. It works. ... I think NumberFormat like that truncates any extra precision. To round, say 2.345 to 2.35, I use BigDecimal. A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea.
🌐
Stack Overflow
stackoverflow.com › questions › 19389676 › convert-from-float-to-integer › 19390298
java - Convert from Float to integer - Stack Overflow
To convert float sensor data to integer you have to mutiply them, and then round. ... Then you have handy acceleration sensor data. ... I just want an integer number , but i don't know how to use it with my java code , thanks 2013-10-15T20:49:40.613Z+00:00 ... double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); }
🌐
Emory
cs.emory.edu › ~cheung › Courses › 255 › Syllabus › 5-repr › IntFloatConv.html
Converting between integer and float data representations
2's complement (integer) representation and · IEEE 754 (float) representation · When you use · int and · float data types in your · high level language program (e.g. Java) and you use the following · statement: The · assignment statement must · first convert one ·