How to round any number to n decimal places in Java?

0 votes
I am dealing with different numerical values. But, I want specific digit decimal values. What will be the solution for this?
Apr 13, 2018 in Java by Daisy
• 8,080 points
117 views

2 answers to this question.

0 votes

You can use setRoundingMode() method to get n digit decimal values.

To write a solution for this question, you have to import 3 libraries.

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Arrays;

After importing these libraries, write the statements given below.

public static void main(String[] args) {

DecimalFormat df = new DecimalFormat("#.##");

df.setRoundingMode(RoundingMode.CEILING);

for (Number n : Arrays.asList(15.00000, 13.23456)) {

    Double d = n.doubleValue();

    System.out.println(df.format(d));

}
}

Output will be: 

15
13.24

answered Apr 13, 2018 by parth295
• 4,640 points
0 votes
new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP);

will get you a BigDecimal. To get the string out of it, just call that BigDecimal's toStringmethod, or the toPlainString method for Java 5+ for a plain format string.

Sample program:

package trials;
import java.math.BigDecimal;

public class Trials {

    public static void main(String[] args) {
        int yourScale = 10;
        System.out.println(BigDecimal.valueOf(0.42344534534553453453-0.42324534524553453453).setScale(yourScale, BigDecimal.ROUND_HALF_UP));
    }
answered Aug 26, 2019 by Sirajul
• 44,960 points

Related Questions In Java

0 votes
2 answers

How to round a double to 2 decimal places?

double value = 200.3456; System.out.printf("Value: %.2f", value); You can ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,880 points
1,240 views
0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 44,960 points
520 views
0 votes
2 answers

How can we display an output of float data with 2 decimal places in java? Please help

Use String.format()  if you need the value as ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 44,960 points
81 views
0 votes
1 answer

How can we format float to n decimal places ?

You may pass the float value: String.format("%.2f", floatVal); READ MORE

answered Jul 2, 2018 in Java by sophia
• 1,400 points
29 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 44,960 points
250 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
81 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,560 points
66 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
102 views
0 votes
2 answers

How to round up a value to 2 decimal places?

double d = 2.34568; DecimalFormat f = new ...READ MORE

answered Aug 30, 2018 in Java by Sushmita
• 6,880 points
97 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,080 points
164 views