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
Answer from Jav_Rock on Stack Overflow
🌐
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
Java Application Developm... ... Use one of these approaches depending on purpose: display formatting, exact decimal arithmetic, or numeric rounding for storage. ... String s = df.format(value); Both produce a string with two decimal places and handle trailing zeros. 2) For rounding to a double/float value (binary floating-point; may produce inexact decimal representation)
Discussions

round up to 2 decimal places in java? - Stack Overflow
1490 How to round a number to n decimal places in Java More on stackoverflow.com
🌐 stackoverflow.com
Truncate to 2 decimal places in Java
The most obvious solution is to multiply by 100, cast to int and multiply by 0.01 but I'm not sure if this could lead to small rounding errors (something I normally don't want to rule out when doing arithmetic with floating point numbers). More on reddit.com
🌐 r/learnprogramming
6
1
December 22, 2021
How to round float to 2 decimal places in Java?
A float of 2.00 is equivalent to a float of 2.0 if you tried to do ==, so that should be fine to return. If you're trying to print a float up to two decimal points you'd probably need to use a printf with %.2f More on reddit.com
🌐 r/csMajors
1
3
November 17, 2020
How do you round a number to N decimal places - Support - Kotlin Discussions
So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
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

🌐
How to do in Java
howtodoinjava.com › home › java basics › round off a float number to 2 decimals in java
Round Off a Float Number to 2 Decimals in Java
February 16, 2024 - float number = 123.456f; System.out.println(roundUp(number, 2)); //123.46 public static double roundUp(double value, int places) { double scale = Math.pow(10, places); return Math.round(value * scale) / scale; } If we only need to display the ...
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › resolved-How-to-make-float-round-to-2-decimal-places-in-tMap › td-p › 2350563
[resolved] How to make float round to 2 decimal places in tMap?
August 23, 2023 - BigDecimal bd = new BigDecimal(2.437732); BigDecimal bd2 = bd.setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("bd2 = "+bd2); and it outputs: Starting job test at 17:13 22/05/2014. connecting to socket on port 3511 connected bd2 = 2.44 ...
Find elsewhere
🌐
Reddit
reddit.com › r/csmajors › how to round float to 2 decimal places in java?
r/csMajors on Reddit: How to round float to 2 decimal places in Java?
November 17, 2020 -

Just did an OA about it and it's driving me crazy. e.g. 1, 2, 3 -> 1.00, 2.00, 3.00

The result needs to be a float, not String.

I tried BigDecimal, DecimalFormat and then use Float.valueOf(<formatted string>), and I can only have 1.0, 2.0, 3.0... for float types that are actually integers. Is it possible in Java?

Sry I don't have a StackOverflow acc so I just post it here, I guess it's kinda related to 'CSMajors'.

🌐
Coderanch
coderanch.com › t › 375591 › java › float-decimal-points
How to round a float value to two decimal points (Java in General forum at Coderanch)
January 12, 2005 - On the other hand, if you want to obtain a float value, rounded to 2 d.p. then converting to text and back might work but would be rather inefficient. An alternative might be to multiply your float by 100.0, cast it to an int and consider it to be in "hundredths". Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma. ... This is what I use. It's not necessarily the best or worst method. Modifying it for 2 decimal places should be pretty easy.
🌐
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
September 24, 2025 - 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. These inaccuracies stem from how floating-point numbers are stored in memory.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing …
🌐
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: 19.55
🌐
BeginnersBook -
beginnersbook.com › home › java › how to round a number to two decimal places in java
How to round a number to two decimal places in Java
May 31, 2024 - setScale(2, RoundingMode.HALF_UP) sets the scale of the BigDecimal to 2 decimal places.RoundingMode.HALF_UP is the rounding mode that rounds towards “nearest neighbour” unless both neighbours are at equal distance, in which case it rounds up. For example, 2.555 becomes 2.56, and 2.554 becomes ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
However, to format the output of a double to two decimal places, simply use the printf method and %.2f as the specifier. public class JavaDoublePrecision { /* Print Java double to 2 decimals of precision.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-round-a-number-to-n-decimal-places
Java Program to Round a Number to n Decimal Places - GeeksforGeeks
January 22, 2026 - Syntax: System.out.format("%.nf", number); n: number of decimal places · number: value to be rounded · Java · class GFG { public static void main(String[] args) { double number = 3.141341435; System.out.format("%.2f", number); } } Output ·
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
The Java BigDecimal class gives you precise control over rounding while avoiding the issues caused by floating-point arithmetic. You can specify both the number of decimal places and the rounding mode. For example, the following code creates a BigDecimal object from the double value and uses the setScale() method to specify two decimal places and the HALF_UP rounding mode: import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double value = 12.34567; int decimalPlaces = 2; BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP); System.out.println("Rounded value: " + bd); } }
🌐
CoreUI
coreui.io › blog › how-to-round-a-number-to-two-decimal-places-in-javascript
How to round a number to two decimal places in JavaScript · CoreUI
February 21, 2024 - The key to rounding to 2 decimal places is to manipulate the number such that the function applies rounding at the correct decimal position, as illustrated through the methods above. How do you round numbers in JavaScript without built-in methods?
🌐
Java2Blog
java2blog.com › home › math › java round double/float to 2 decimal places
java round double/float to 2 decimal places - Java2Blog
May 12, 2021 - This tutorial provides round double/float to 2 decimal places in java with the help of Math.round and DecimalFormat.
🌐
Swift Forums
forums.swift.org › using swift
Rounding Float to two decimal places - Using Swift - Swift Forums
August 14, 2019 - I have a workaround, but I would ... let numberOfPlaces = 2.0 let multiplier = pow(10.0, numberOfPlaces) let rounded = round(Double(totalYield) * m......