Rounding a double to turn it into an int (java)

JavaDoubleIntRounding

Java Problem Overview


Right now I'm trying this:

int a = round(n);

where n is a double but it's not working. What am I doing wrong?

Java Solutions


Solution 1 - Java

What is the return type of the round() method in the snippet?

If this is the Math.round() method, it returns a Long when the input param is Double.

So, you will have to cast the return value:

int a = (int) Math.round(doubleVar);

Solution 2 - Java

If you don't like Math.round() you can use this simple approach as well:

int a = (int) (doubleVar + 0.5);

Solution 3 - Java

Rounding double to the "nearest" integer like this:

1.4 -> 1

1.6 -> 2

-2.1 -> -2

-1.3 -> -1

-1.5 -> -2

private int round(double d){
	double dAbs = Math.abs(d);
	int i = (int) dAbs;
	double result = dAbs - (double) i;
	if(result<0.5){
		return d<0 ? -i : i;			
	}else{
		return d<0 ? -(i+1) : i+1;			
	}
}

You can change condition (result<0.5) as you prefer.

Solution 4 - Java

The Math.round function is overloaded When it receives a float value, it will give you an int. For example this would work.

int a=Math.round(1.7f);

When it receives a double value, it will give you a long, therefore you have to typecast it to int.

int a=(int)Math.round(1.7);

This is done to prevent loss of precision. Your double value is 64bit, but then your int variable can only store 32bit so it just converts it to long, which is 64bit but you can typecast it to 32bit as explained above.

Solution 5 - Java

import java.math.*;
public class TestRound11 {
  public static void main(String args[]){
    double d = 3.1537;
    BigDecimal bd = new BigDecimal(d);
    bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
    // output is 3.15
    System.out.println(d + " : " + round(d, 2));
    // output is 3.154
    System.out.println(d + " : " + round(d, 3));
  }

  public static double round(double d, int decimalPlace){
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
  }
}

Solution 6 - Java

Documentation of Math.round says:

> Returns the result of rounding the argument to an integer. The result > is equivalent to (int) Math.floor(f+0.5).

No need to cast to int. Maybe it was changed from the past.

Solution 7 - Java

public static int round(double d) {
    if (d > 0) {
        return (int) (d + 0.5);
    } else {
        return (int) (d - 0.5);
    }
}

Solution 8 - Java

You really need to post a more complete example, so we can see what you're trying to do. From what you have posted, here's what I can see. First, there is no built-in round() method. You need to either call Math.round(n), or statically import Math.round, and then call it like you have.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - JavaMihir MathuriaView Answer on Stackoverflow
Solution 2 - JavaDr. Daniel ThommesView Answer on Stackoverflow
Solution 3 - JavavalerybodakView Answer on Stackoverflow
Solution 4 - JavaDivyansh RaiView Answer on Stackoverflow
Solution 5 - JavaScottView Answer on Stackoverflow
Solution 6 - Javathug-gamerView Answer on Stackoverflow
Solution 7 - JavaAlexander CybermanView Answer on Stackoverflow
Solution 8 - JavaJoey GibsonView Answer on Stackoverflow