Convert double to Int, rounded down - java

How to convert a double value to int doing the following:
Double If x = 4.97542. Convert to int x = 4.
Double If x = 4.23544. Convert to int x = 4.
That is, the answer is always rounding down.

If you explicitly cast double to int, the decimal part will be truncated. For example:
int x = (int) 4.97542; //gives 4 only
int x = (int) 4.23544; //gives 4 only
Moreover, you may also use Math.floor() method to round values in case you want double value in return.

If the double is a Double with capital D (a boxed primitive value):
Double d = 4.97542;
int i = (int) d.doubleValue();
// or directly:
int i2 = d.intValue();
If the double is already a primitive double, then you simply cast it:
double d = 4.97542;
int i = (int) d;

double myDouble = 420.5;
//Type cast double to int
int i = (int)myDouble;
System.out.println(i);
The double value is 420.5 and the application prints out the integer value of 420

Another option either using Double or double is use Double.valueOf(double d).intValue();. Simple and clean

I think I had a better output, especially for a double datatype sorting.
Though this question has been marked answered, perhaps this will help someone else;
Arrays.sort(newTag, new Comparator<String[]>() {
#Override
public int compare(final String[] entry1, final String[] entry2) {
final Integer time1 = (int)Integer.valueOf((int) Double.parseDouble(entry1[2]));
final Integer time2 = (int)Integer.valueOf((int) Double.parseDouble(entry2[2]));
return time1.compareTo(time2);
}
});

Related

Displaying result as an int in a text view

I have the following code which displays my result as a double however I want to display it as an int? This is my already working code
Bundle b = getIntent().getExtras();
double result = b.getDouble("total");
TextView display = (TextView)findViewById(R.id.txtResult);
display.setText(+result+ "well done");
You can cast it if that result is a double primitive, which is forcing the double to be an integer, losing the decimal part....
display.setText((int)result+ "well done");
if result is a Double wrapper, then call the method Double#intValue()
display.setText(result.intValue()+ "well done");
Which one do you want:
1-Double to Integer
2-Double to int
3-double to int
4-double to Integer
1- Convert Double to Integer is not possible directly. you have to convert Double to double , then convert double to int and at the end convert int to Integer
Double D= Double.valueOf(5.0);
double d= D.doubleValue();
int i=(int)d;
Integer I= Integer.valueOf(i);
you can Proceeding line 2 and 3 like this :
int i= D.intValue();
2- Double to int
Double D= Double.valueOf(5.0);
int i= D.intValue();
3- double to int :
double d;
int i=(int)d
aslo you can use round for line 2:
int i=Math.round(d);
4- double to Integer
double d;
int i=(int)d
Integer I= Integer.valueOf(i);
again you can use round for line 2:
int i=Math.round(d);
you should set Text like below :
display.setText((int)result + "well done");

Convert/Cast Double to int? (Object to primitive type)

How do I convert/cast a Double object to an int?
Double d = new Double(12.34);
int i = 12 //is what I'm looking for
If I had a double I would just use:
double z = 12.34;
int i = (int)z;
Note: I want it to truncate the Double (so 12.9 becomes 12) because I know that d actually was an integer that was converted to a Double.
I.e. I want to call (int) on a Double.
To perform this operation you need to call the intValue method on the Double object:
int i = d.intValue();
This method performs a cast, into an int value, on the double value wrapped within the Double object, d.
Note that it does not check for null before attempting this.
If you want to use a cast you can use
Double d = 12.34;
int i = (int) (double) d;
You can't cast from Double to int in one step.

How to covert a double to an int

I'm trying to convert a double to an int but I don't get my expected results. Can someone help me?
Here's my code. My expected output is 21 but I keep getting 21.0.
double costItem2 = 32.55;
int budget2 = 713;
double totalItem2 = budget2 / costItem2;
totalItem2 = (int) totalItem2;
System.out.println(totalItem2);
Thats because double totalItem2 still holds a double even if you cast the result that you're assigning it to an int
You have to:
int totalItemTemp2 = (int) totalItem2
Your best option is to format the output yourself. Here is how:
NumberFormat numFormat = new DecimalFormat("#.####"); // you can have it as #.## if you only want up to two decimal places
double costItem2 = 32.55;
int budget2 = 713;
double totalItem2 = budget2 / costItem2;
System.out.println(numFormat.format(totalItem2));
so for example, 123.00 would be printed as 123
casting totalItem2 to int will not change the type of totalItem2(which will still remain a double).
int tmpInt = (int) totalItem2;
System.out.println(tmpInt);
should fix it.
You can't change the type of totalItem2 at runtime like this,
double totalItem2 = budget2 / costItem2; // <-- totalItem2 is a double, not int
totalItem2 = (int) totalItem2; // <-- truncates
But you could change the declaration to an int with a cast like,
int totalItem2 = (int) (budget2 / costItem2); // <-- truncates
or a long with out,
long totalItem2 = Math.round(budget2 / costItem2); // <-- rounds, no cast!
or an int bu using a float costItem2,
float costItem2 = 32.55f;
int totalItem2 = Math.round(budget2 / costItem2); // <-- rounds, no cast!

convert double into int

I have following double value:
8.943 need to convert(cast) into int e.g: 8.943*1000000 = 8943000
required value --> 8943000
please help!
thanks.
double myDouble = 8.943
int myInt = (int) (myDouble * 1000000)
Using (int) casts the double into an int

How do I get more than one decimal spot?

I'm making a simple calculator (I'm a beginner), and I was wondering why when I divide 1 by 4, I get 0. I know it has something to do with the type of number. Here is my code:
private static void calc(int a, int b,String op){
if (op.equals("add")){
double ans = a+b;
System.out.println(ans);
}
if(op.equals("subtract")){
double ans=a-b;
System.out.println(ans);
}
if(op.equals("multiply")){
double ans=a*b;
System.out.println(ans);
}
if(op.equals("divide")){
double ans=a/b;
System.out.println(ans);
}
}
I can't get my variable (ans) to have more than one decimal spot. And when the answer requires one (like 1/4), it just returns 0.
Help please.
You need to divide on a double since all your result values in double.
private static void calc(int a, double b,String op){
}
Or simply :
if(op.equals("divide")){
double ans=(double)a/b;
System.out.println(ans);
}
Although the other comments and answers are correct, they do not give enough detail (IMO).
Although ans is a double, the value of the right side of the equation is still an int.
double ans = a / b;
// equiv to:
// double ans = (double)(a / b);
So when you evaluate this you get:
double ans = (double)(1/4);
= (double)(0);
= 0.0d;
To get around this, you will need to cast one or both of the operands to a double.
double ans = ( (double) a ) / ( (double) b );
= ( (double) 1 ) / ( (double) 4 );
= 1.0d / 4.0d;
~= 0.25d;
Also note with regards to #Salah's answer that the cast from integer to double has just been moved. Since you can implicitly convert an int to a double, calling the function with an integer (such as the literal 1) will automatically convert the integer to a double. However, you may not want to allow non-integral numbers to be passed to your function, and if so, then this solution would not be correct.

Categories

Resources