Need some help with decimal points


#1

package homework;

import java.util.Scanner;

public class Homework {

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
   int x,y, result;
   
   System.out.print("Enter first number :"); 
        x = input.nextInt();
   
   System.out.print("Enter second number :");
        y = input.nextInt();
   
   result = x + y; 
        System.out.printf("The result of adding is %d\n",result);
   
   result = x - y; 
        System.out.printf("The result of subtracting is %d\n",result);
   
   result = x * y ;
        System.out.printf("The result of multiplying is %d\n",result);
   
   result = x / y;
        System.out.format("The result of dividing is %d\n",result );
   
  
}

}

currently this is my code how do i get the dividing to print out with decimal point eg first number is 4 , second number is 2 i want the last result to be 2.0 instead of just 2


#2

No decimals are stored in your result, it’s an integer
You could add ".0" in your string.
Or, you could use doubles instead (possibly only for division), for which there is a format specifier for showing a fixed number of decimals (easily googled)


#3

yeap i changed all to double , and added .0f to the others and added 1.1 to dividing and got the results i wanted thanks!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.