I'm working on a scaling system, to scale numbers up 25% and round to the nearest integer, and to do this, I am trying to rely on the loss of precision by transferring a double into an int. Is there any way to get around this? or should I go about it in a different way?
public int[] scaleMarks(int[] marks)
{
double scale = 1.25;
double temp1;
int temp2;
for(int i = 0; i < marks.length; i++)
{
temp1 = marks[i] * scale;
temp2 = marks[i] * scale; //***Loss of precision here***
if(temp1-temp2>= 0.5)
{
temp2++;
}
marks[i] = temp2;
}
return marks;
}
You need to round the product with
Math.round
so that it can be casted to anint
safely.