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
🌐
Mkyong
mkyong.com › home › java › java – how to round double / float value to 2 decimal places
Java – How to round double / float value to 2 decimal places - Mkyong.com
February 25, 2025 - This approach gives us more control over the rounding mechanism and minimizes errors caused by floating-point arithmetic. ... package com.mkyong.math.rounding; import java.math.BigDecimal; import java.math.RoundingMode; public class BigDecimalExample { public static void main(String[] args) { double input = 1205.6358; System.out.println("Original double value : " + input); // Convert double to BigDecimal BigDecimal salary = new BigDecimal(input); System.out.println("BigDecimal value : " + salary); // Round to 2 decimal places using RoundingMode.HALF_UP BigDecimal salaryRounded = salary.setScal
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

Discussions

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 23, 2021
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 a number to n decimal places in Java - Stack Overflow
What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the sta... More on stackoverflow.com
🌐 stackoverflow.com
how to limit or round a float to only two decimals without rounding up
You can try the solution here: https://stackoverflow.com/a/62435913 More on reddit.com
🌐 r/learnpython
12
5
March 8, 2024
🌐
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
January 15, 2025 - 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); } }
🌐
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 23, 2021 -

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'.

🌐
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)
🌐
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
🌐
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 ·
🌐
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 ...
🌐
Java67
java67.com › 2020 › 04 › 4-examples-to-round-floating-point-numbers-in-java.html
4 Examples to Round Floating-Point Numbers in Java up to 2 Decimal Places | Java67
DecimalFormat df = new ... Before rounding, original numbers : [2.123, 2.125, 2.127] After rounding using Math.round() method : [2.12, 2.13, 2.13] Before rounding numbers : [2.123, 2.125, 2.127] After rounding number ...
🌐
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)
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.
🌐
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.
🌐
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
2 weeks ago - 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.
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Round to 2 Decimal Places in Java - Lesson | Study.com
January 5, 2018 - Because the Math.round function rounds to the nearest whole number, we will first multiply the base * rate by 100.0. The .0 tells Java we intend to deal with a float or double value.
🌐
BeginnersBook
beginnersbook.com › 2024 › 05 › 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 - public class RoundExample { public static void main(String[] args) { double doubleValue = 143.456789; float floatValue = 143.456789f; String roundedDouble = String.format("%.2f", doubleValue); String roundedFloat = String.format("%.2f", floatValue); // Output: 143.46 System.out.println("Rounded double: " + roundedDouble); // Output: 143.46 System.out.println("Rounded float: " + roundedFloat); } } In this approach, we create an instance of DecimalFormat with the pattern #.00. The pattern #.00 indicates that the number should have at least one digit before the decimal point and exactly two digit
🌐
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
🌐
Javatpoint
javatpoint.com › how-to-round-double-and-float-up-to-two-decimal-places-in-java
How to Round Double and Float up to Two Decimal Places in Java - Javatpoint
How to Round Double and Float up to Two Decimal Places in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Java2Blog
java2blog.com › home › number › 7 ways to print float to 2 decimal places in java
7 ways to print float to 2 decimal places in java - Java2Blog
May 2, 2021 - You can provide formatting pattern to DecimalFormat class to print float to 2 decimal places. Here, RoundingMode can be used to control rounding behavior.