How to round a double to 2 decimal places?

0 votes

If the value is 300.1234, it should be formatted to 300.12. If it is 300, then it should be 300.00

Apr 26, 2018 in Java by sophia
• 1,400 points
72 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
public static double round(double val, int places) 
{
    if (places < 0) throw new IllegalArgumentException();

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

answered Apr 26, 2018 by Akrati
• 3,150 points
0 votes
double value = 200.3456;
System.out.printf("Value: %.2f", value);

You can use either String.format()

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

Or you can use DecimalForamat()

DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
answered Dec 11, 2018 by Sushmita
• 6,820 points

Related Questions In Java

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
1 answer

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

You can use setRoundingMode() method to get ...READ MORE

answered Apr 13, 2018 in Java by parth295
• 4,590 points
51 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
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,360 points

edited Nov 6, 2018 by Omkar 4,470 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,030 points
1,205 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,030 points
3,465 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,030 points
214 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,020 points
24 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by parth295
• 4,590 points
35 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.