< double anioB = 0;
AnioB = subtotal / 1461;
System.out.println (String.format ("%. 3f", anioB));
// pulls 503,000 with subtotal 734952 and actually it should be 503.0472 .... >
< double anioB = 0;
AnioB = subtotal / 1461;
System.out.println (String.format ("%. 3f", anioB));
// pulls 503,000 with subtotal 734952 and actually it should be 503.0472 .... >
This happens because computers work in binary and when working with variables of type float
or double
they make precision errors when working with values that they can not represent. For example, if we want to represent 1/3, the result is 0.3333333333333 ... and so on to infinity. We can not give an exact value to that operation. Java gives us BigDecimal
a class designed to solve this type of problems.
BigDecimal resul, num, den;
//Divide num/den con una escala decimal 5 con el método de redondeo CEILING
resul = num.divide(den, 5, RoundingMode.CEILING)
System.out.println(resul.toString());