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
748 views

2 answers to this question.

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,880 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,880 points
69 views
0 votes
2 answers

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

new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

answered Aug 26 in Java by Sirajul
• 37,080 points
93 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 in Java by Sirajul
• 37,080 points
57 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
19 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,380 points

edited Nov 6, 2018 by Omkar 10,148 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,110 points
2,278 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,110 points
5,916 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,110 points
690 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,050 points
55 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,640 points
55 views