How do I make the division of 2 integers show up as a number with their digits in JAVA

0

< 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 .... >

    
asked by Whor 11.11.2017 в 17:22
source

1 answer

0

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());
    
answered by 11.11.2017 / 19:06
source