If you are concerned about precision
- don't use
float
, it has the lowest precision of any option available. I suggest usingdouble
orBigDecimal
- use operation which involve values which can be accurately represented.
0.1 * x
will give you error because0.1
cannot be represented precisely. Usingx / 10.0
will have less error.
I would write something like this
double levinWeight = Double.parseDouble(dataOnlyCombine[2]);
double lsmWeight = Double.parseDouble(dataOnlyCombine[3]);
double totalWeight = (levinWeight + 8 * lsmWeight) / 10.0;
// perform rounding only at the end as appropriate.
// to round to two decimal places
double totalWeight2 = Math.round(totalWeight * 100) / 100.0;