Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

For example:

Use with AIround(200.3456, 2); // returns 200.35

Original version; watch out with this

Use with AIpublic static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

I've been using the above to round "not-too-big" doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it's best to avoid it, since more reliable ways are readily available, with cleaner code too.

So, use this instead

(Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

Use with AIpublic static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = BigDecimal.valueOf(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

Note that HALF_UP is the rounding mode "commonly taught at school". Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

Of course, if you prefer, you can inline the above into a one-liner:
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

And in every case

Always remember that floating point representations using float and double are inexact. For example, consider these expressions:

Use with AI999199.1231231235 == 999199.1231231236 // true
1.03 - 0.41 // 0.6200000000000001

For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

Use with AISystem.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

Some excellent further reading on the topic:

  • Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch
  • What Every Programmer Should Know About Floating-Point Arithmetic

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

Answer from Jonik on Stack Overflow
Top answer
1 of 13
963

Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

For example:

Use with AIround(200.3456, 2); // returns 200.35

Original version; watch out with this

Use with AIpublic static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

I've been using the above to round "not-too-big" doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it's best to avoid it, since more reliable ways are readily available, with cleaner code too.

So, use this instead

(Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

Use with AIpublic static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = BigDecimal.valueOf(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

Note that HALF_UP is the rounding mode "commonly taught at school". Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

Of course, if you prefer, you can inline the above into a one-liner:
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

And in every case

Always remember that floating point representations using float and double are inexact. For example, consider these expressions:

Use with AI999199.1231231235 == 999199.1231231236 // true
1.03 - 0.41 // 0.6200000000000001

For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

Use with AISystem.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

Some excellent further reading on the topic:

  • Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch
  • What Every Programmer Should Know About Floating-Point Arithmetic

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

2 of 13
403

If you just want to print a double with two digits after the decimal point, use something like this:

Use with AIdouble value = 200.3456;
System.out.printf("Value: %.2f", value);

If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:

Use with AIString result = String.format("%.2f", value);

Or use class DecimalFormat:

Use with AIDecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
🌐
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 - package com.mkyong.math.rounding; ... // Using String.format to round to 2 decimal places System.out.println("Rounded value : " + String.format("%.2f", input)); System.out.format("Rounded value : %.2f", input); } ...
People also ask

What is the most straightforward method to round numbers in C# to two decimal places?
The Math.Round method in C# is a straightforward way to round numbers to two decimal places by specifying the number to be rounded and the desired number of decimal places.
🌐
ironpdf.com
ironpdf.com › ironpdf blog › .net help › csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
What are the differences between decimal and double types in C#?
In C#, decimal is used for high precision calculations, often in financial contexts, while double is used for general floating-point calculations where performance is prioritized over precision.
🌐
ironpdf.com
ironpdf.com › ironpdf blog › .net help › csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
How does C# handle rounding when a number is at the midpoint?
C# allows developers to specify the rounding behavior when a number is at a midpoint through the MidpointRounding enum, such as using MidpointRounding.AwayFromZero to round up midpoints.
🌐
ironpdf.com
ironpdf.com › ironpdf blog › .net help › csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
🌐
Baeldung
baeldung.com › home › java › java numbers › truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
September 2, 2026 - To truncate a positive number to two decimal places, we first multiply our double by 100, moving all the numbers we want to keep in front of the decimal place. Next, we use Math.floor() to round down, removing everything after the decimal place.
🌐
How to do in Java
howtodoinjava.com › home › java basics › round a double to 2 decimal places in java (or n places)
Round a Double to 2 Decimal Places in Java (or N Places)
February 16, 2024 - A shipping service that bills per started kilogram uses CEILING, because 2.01 kg must cost the same as 3 kg. The quickest way without BigDecimal is to multiply by 100, round to a whole number with Math.round() and divide by 100.0 again.
🌐
IronPDF
ironpdf.com › ironpdf blog › .net help › csharp-round-to-2-decimal-places
C# Rounding to 2 Decimal Places (Beginner-Friendly Guide)
August 2, 2026 - In this example, the decimal value 3.14519 is rounded to 3.15. The method Math.Round takes the decimal d and rounds it to two decimal places. Rounding a double value works similarly to rounding a decimal value.
Find elsewhere
🌐
Calculator Academy
calculator.academy › home › math and numbers › round to 2 decimal places calculator
Round to 2 Decimal Places Calculator (+ Rounding Modes)
3 weeks ago - To round a number to two decimal places, multiply it by 100, round to the nearest whole number, then divide by 100: ... Set “Round to” to 2 decimal places for the standard case, to Decimal places when you want a different count, to Significant ...
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java – Display double in 2 decimal places | mkyong.com
October 29, 2021 - We also can use String formater / to round the double to 2 decimal places.
🌐
Coder Legion
coderlegion.com › 9697 › how-to-round-off-double-value-to-2-decimal-places-in-c
How to round off double value to 2 decimal places in C++ - Coder Legion
January 14, 2026 - Preciseness is crucial in programming, particularly when working with numerical quantities. It's common to need to round a double to a certain number of decimal places in a variety of fields, from scientific calculation to banking. We will look into...
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Java: How to Round a Number to N Decimal Places | Sentry
September 11, 2026 - 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); } }
🌐
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 - In the following program, In the format specifier %.2f, the f indicates decimal while point (.2) indicates the two decimal places. This method returns a String representation of the rounded number. 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); } }
🌐
DataCamp
datacamp.com › home › tutorials › python › round to 2 decimal places in python: 7 methods explained
Round to 2 Decimal Places in Python: 7 Methods Explained | DataCamp
June 2, 2026 - The example below prints 34.15. # Example number to be rounded number = 34.14559 # Rounding the number to 2 decimal places rounded_number = round(number, 2) print(rounded_number) # 34.15Powered by
🌐
Medium
medium.com › @mihirdesai33 › how-to-round-a-double-to-2-decimal-places-in-c-8c50770d1b0e
How to round a double to 2 decimal places in C | by Mihir Desai | Medium
July 13, 2023 - You can use this function along with some math to round a double to two decimal places. The idea is to multiply the double by 100, round it to the nearest integer, and then divide it by 100 to obtain the rounded result.
🌐
Onextdigital
onextdigital.com › how-to-round-doubles-in-java-to-two-decimal-places
Easy ways to round doubles in java to two decimal places
July 19, 2023 - Since doubles are stored with limited precision in memory, performing comparisons between two double values that are expected to be equal may return unexpected results due to rounding errors. Rounding the values to two decimal places can help mitigate this issue.