Table of Contents

This guide will introduce several ways to round a double to two decimal places in Java. There are multiple mathematical classes you can employ to get the job done.

**Round A Double To Two Decimal Places In Java**

**With Math.round()**

The Math class comes with several methods for carrying out numeric operations, including rounding with the `round()`

method.

**Syntax**:

`public static long round(double a)`

This method rounds the double value a to the closest long value. If you provide a NaN value, it returns 0.

When given positive infinity, negative infinity, or any value that is outside of the range of long values, the result will be either `Long.MIN_VALUE`

or `Long.MAX_VALUE`

. The actual value will depend on your Java implementation and your platform specifications.

Keep in mind that the original purpose of the `round()`

method is to get a long value, which is an integer number. Because we need only two decimal places, we need to do the trick.

We will multiply the double value by 100 first to move the decimal to the right two places. After applying the `round()`

method, we divide the result again to get back the floating-point value, which will be rounded to two decimal places.

Here is how you can implement this idea in Java:

```
import java.util.*;
import java.lang.*;
class Main
{
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter a double value: ");
String input = myObj.nextLine();
double d = Double.parseDouble(input);
double roundDbl = Math.round(d*100.0)/100.0;
System.out.println("Rounded value: "+roundDbl);
}
}
```

**Output**:

```
Enter a double value:
3.14159265359
Rounded value: 3.14
```

In this example, we use a Scanner to ask the user to enter a number, which will be converted to a double value with the `Double.parseDouble()`

method. We use the algorithm above to round it to 2 decimal places and print the result.

**With BigDecimal.setScale()**

The BigDecimal class offers operations for format conversion, hashing, comparison, rounding, scale manipulation, and arithmetics.

Compared to the Math class, it can handle very large and very small floating-point numbers much better. This class gives you total control over rounding behaviors. You can choose the precision as well as rounding mode, thanks to the MathContext object.

In particular, the `setScale()`

method converts a BigDecimal to a new scale with this syntax:

`public BigDecimal setScale(int newScale, RoundingMode roundingMode)`

**Parameters**:

**newScale**: the scale you want the method to return.**roundingMode**: the rounding mode you want the method to apply.

The method will return a BigDecimal with the scale you specify. In this case, we need the scale of 2 and the `HALF_UP`

rounding mode. This is how you can rewrite the previous code with `setScale()`

:

```
import java.util.*;
import java.lang.*;
import java.math.*;
class Main
{
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter a double value: ");
String input = myObj.nextLine();
double d = Double.parseDouble(input);
BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_UP);
double roundDbl = bd.doubleValue();
System.out.println("Rounded value: "+roundDbl);
}
}
```

**Output**:

```
Enter a double value:
3.14159265359
Rounded value: 3.14
```

We need to use the `doubleValue()`

method to return a double value after doing the conversion in the BigDecimal class.

**With DecimalFormat**

`DecimalFormat`

is a subclass of `NumberFormat`

, which can help you format decimal numbers. It supports a wide variety of locales, including Western digits.

You will need to provide a pattern, which includes a suffix, numeric part, and a prefix. There are positive and negative subpatterns, with the latter optional.

**Example**:

```
import java.util.*;
import java.lang.*;
import java.text.DecimalFormat;
class Main
{
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter a double value: ");
String input = myObj.nextLine();
double d = Double.parseDouble(input);
DecimalFormat df = new DecimalFormat("###.##");
System.out.println("Rounded value: "+df.format(d));
}
}
```

**Output**:

```
Enter a double value:
3.14159265359
Rounded value: 3.14
```

**Summary**

The Math and BigDecimal classes allow you to round a double to two decimal places in Java with the `round()`

and `setScale()`

methods, respectively. You can also use the DecimalFormat subclass.The Math and DecimalFormat classes apply to the double value itself, but you will need to convert it to a BigDecimal value if you want to use the `setScale()`

method. To learn more about other math functions in Java, check out this guide.

## Leave a comment