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,020 points
51 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,590 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,820 points
72 views
0 votes
1 answer

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

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
100 views
0 votes
1 answer

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

You can use the printf method, for ...READ MORE

answered Jun 4, 2018 in Java by Daisy
• 8,020 points
23 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
6 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
43 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
28 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,520 points
32 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
47 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,820 points
28 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,020 points
78 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.