Converting a variable from Double to Int - java

Lets say that I have variable tmp which is a double, and I want to convert tmp to an int, but still have the variable called tmp. How would I do this?
Thanks

You can't have the same variable as both an int and a double.
You can have this though:
double d = 0.1d;
int i = (int) d;
d = (double) i;
System.out.println(d);
Basically you first cast your double to an integer, losing the fraction. Afterwards you cast it to a double again and assign it to your d variable. You don't have to explicitly do the casting from int to double because it is a widening conversion, but it makes it more clear what happens.
The end result is that your d variable now has a value that can be precisely interpreted by an integer but on the other hand you also, well, basically threw away your fraction. Your variable did not change its type, however.
You can write this less verbosely like this:
double d = (int) 0.1d;

Related

Java variable assignment to a double. Why is the value converted to a double?

int i, j, k;
double a, b, c;
The following sequence of statements is correct:
i = 3;
a = i + 5;
b = 12 / a;
c = i + b;
c = c - b;
j = 12 / 3;
a = 12 / 3;
b = 13 / 3;
// a gets the value 8.0
// b gets the value 1.5
// c gets the value 4.5
// c now gets the value 3.0
// j gets the value 4;
// a gets the value 4.0
// b gets the value 4.0!
In the last line, why does b get the value 4.0? 13 / 3 in Java is 4 isn't it? Why does the 4 get converted to a double? Is that what the variable type does to the value? I thought doubles can hold integers?
What you have here is called assignment conversion.
Assignment conversion occurs when the value of an expression is
assigned to a variable: the type of the expression must be
converted to the type of the variable.
Assignment contexts allow the use of one of the following:
an identity conversion
a widening primitive conversion
a widening reference conversion
a boxing conversion optionally followed by a widening reference conversion
an unboxing conversion optionally followed by a widening primitive conversion.
In this case, your assignment requires converting the value of the expression into double because that's the type of the target of the assignment. The conversion required is widening primitive conversion, which changes the value of the expression into a type that has a wider range that includes that value.
In converting from int to double, you don't lose precision (a double can hold all the values of type int including all their digits, which a float can't). But that doesn't mean that the value is still an int. It has been converted to the double that represents the same value.
A variable of type X can never hold a value of any type other than X (except polymorphically, but that doesn't apply to primitive types like int and double).
it works like this
b = 13 / 3 => b = int / int . So it takes 13/3 int and then it converts it to double.
if you want the correct you should do it like b = (double)13/(double)3;
Since in 13/3 both values are integers the calculation is done in integer arithmetic and yields the (integer) value 4. Since b can only hold double values, it is assigned the (double) value 4, which depending on your method to display the value will be shown as 4.0
The precision is already gone by the time you assign it.
You need to cast to double
b = ((double)13)/3
if you write:
b = (double)13 / 3;
or
b = 13. / 3;
it won't do that. I think you know now why.

Double value is boxed and then immediately unboxed in Java

How can I avoid using all that casts in my code in order not to have my value boxed/unboxed by sonar ?
Here's my code :
Double value = (Double) input.get(0);
int integer = new Double(value).intValue();
Double dec = Math.abs(value - (new Double(integer ).doubleValue()));
I would like to avoid casting as much as I do, but I am not sure I can keep it boxed this way.
Any ideas ?
Thank you.
I'm not sure what type input.get() returns so I'll assume that the first cast is necessary. You don't need to instantiate new Double objects on every line, you can just call its methods to return the correctly-typed values you need.
Double value = (Double) input.get();
int integerPart = value.intValue();
double decimalPart = Math.abs(value - integer.doubleValue());
It looks like you're just trying to extract the integer and decimal parts of a double, in which case this code expresses the intent a bit more clearly:
Double value = (Double) input.get();
int integerPart = value.intValue();
double decimalPart = Math.abs(value - Math.floor(value));

how to convert data types of variables in java

I have a problem that I want to display double type data in integer type when there is no any . (dot) in data or there is . (dot) in data then display float type data. In java
i.e.
double a=2;
double b=4;
double sum=a+b;
sum=6.0
but i want sum=6
OR
double a=2.1;
double b=4;
double sum=a+b;
sum=6.1
but i want sum=6
You can use the cast syntax. Note that this would raise a warning in most compilers/IDEs, as you are losing precision here:
int sum = (int)(a + b);
use a cast...
(int)double or else you can declare an int variable and assign that to double.You need to add cast as there is loss of precision
int value =(int)double_val
you have to cast your variable to an integer:
try it out like this:
System.out.println((int)(66.314));
System.out.println((int)(sum));
Your sum variable should be casted into an integer...
Try this:
double a=2.1;
double b=4;
double sum=a+b;
int sumInt=(int)sum;
use casting in Java to print out your result.
For more information , please see
http://howtoprogramwithjava.com/java-cast/

Implicit typecasting - Int to double

I've got an excercise from university which looks like:
int a = 10;
int b = 3;
double c = a / b;
The question is: Which value is c.
Now I would say, c is 3.3. It's casted implicit to double before calculating the result.
But the correct answer to this question is according to my records 3.0.
How this can be? Does the compiler really calculate the result first as integer and then in a second step casts it to double?
Or did I understand that incorrectly?
Does the compiler really calculate the result first as integer and
then in a second step casts it to double?
Yes
Does the compiler really calculate the result first as integer and
then in a second step casts it to double?
Yes,
Runtime first calculates the RHS result and then converts the result to double. Now in your case as RHS contains int / int so the result is in int and you don't get 3.3.
So if RHS contains double / int or int / double, the type promotion occurs and RHS operands are promoted to double before calculating the result and hence you get 3.3
See what is actually happening is :
double c = (double) a / b; //double of 3 = 3.0
you have to do
double c = a/(double)b

Java - division of small number by big number

I have declared a variable as double. I wanted to do division of two integer and assign the output to that double variable.
But its not considering the values like 0.1 to 0.9. Only when the no is whole number like 4.0 its returning the answer
public static void main(String[] args) throws Exception
{
double itf=0;
a=4100;
b=6076
itf=a/b;
System.out.println("itf:"+itf)
}
Output is itf: 0.0
Pls help..
Most likely the variables a and b are defined as int , which will result in integer division result as 0 and when assigned to double it becomes 0.0. If you define a,b and itf as double then the result should be
0.6747860434496379
instead of
0.0
Try this code:
public static void main(String[] args) throws Exception {
double itf = 0;
double a = 4100;
double b = 6076;
itf = a / b;
System.out.println("itf:" + itf);
}
a and b are both integers, so the result of a/b is also an integer, which is then cast to a double to be stored in itf. To get the correct result, you could either define a and b as doubles, or you could cast at least one of them to double. For instance:
itf = (double)a/b;
Declare a or b as double.
OR
Cast the operation.
itf = (double)(a/b)
OR Cast one of the integers.
itf = ((double)a)/b
OR multiply with 1.0, which is double.
itf = (1.0*a)/b
The reason for your result is that you are using integer division, then assign the result to a double value. This is your code unrolled:
int a = 4100;
int b = 6076;
int temp = a / b;
double itf = temp;
If you want to explicitly use floating point division, you need to tell the compiler that you do by casting at least one (or all to be sure) member of that operation to double or float. Example:
itf = (double)a / (double)b;
Use the foloving cast to at least one operand to double
itf=((double)a)/b;
or
itf=a/((double)b);
double itf = ((double) a / (double) b);

Categories