Question

I have this code:

import java.util.Scanner;

public class DecimalPoints {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);

        System.out.println("a: ");               // printing output
        String text_1 = scanner.nextLine();      // taking input
        double d1 = Double.parseDouble(text_1);  // parsing double from input  

        System.out.println("b: ");               // printing output
        String text_2 = scanner.nextLine();      // taking input
        double d2 = Double.parseDouble(text_2);  // parsing double from input

        double d3 = d1 / d2;

        d3 = Math.round(d3 * 100) / 100.0d;

        System.out.println("Result: " + d3);
    }
}

It takes two inputs and rounds it up to two decimal places but the problem is that it rounds up the result. Let's say that I enter a = 513 and b = 791 the result would be 0.65 but when I check my calculator it shows me 0.648...Does anyone know how to leave the number just at 0.64? Much Obliged.

Was it helpful?

Solution

Maybe use Math.floor() instead of Math.round()?

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top