Guys Convert your data type before doing calculation ..
Here is the Code :
.
.
Integer i =
12
;
Decimal d = i /
5
;
System.debug(d);
According to you what should be the right answer 2.4? Nopes current answer is 2.
Let me explain to you..
Decimal is a real number, so it can definitely hold 2.4. But the problem is, i is an integer, and 5 is an integer. So Apex will consider the type of i / 5 to be an integer because of that!
So the above code can be translated as:
Integer i = 12;
Integer temp = i / 5;
Decimal d = temp;
System.debug(d)
Now, make sense?
Okay, what if we want to fix it? We should do the data conversion first, and the calculation later. Below is the code.
Integer i = 12;
Decimal decVersion = i;
Decimal d = decVersion / 5; System.debug(d);
This will output 2.4
0 comments: