Question about how Modulus works
calistoknight Sep 24, 2012 12:46 AMHi there I just started programing and was practicing by making a program that converts meters into inches, feet, and miles. I finished it up and decided that rather than having feet be in decimal I wanted to make it look better by cutting off the decimal and converting it to inches.
What however my formula which was inches%12 wasn't giving me just the decimal. It gives me the decimal in inches. So using 10 meters converting it into inches I get 393.7 inches and then divided by 12 gives you 32.8 and some change. But instead of just giving me that .8 and change it was giving me .8x12 to get 9.6 and change.
It's what I want in the end but I'm completely confused on how the program is getting it when it shouldn't ( I think?)
My Code (I entered the remain line since I wanted to see the actual value.
--------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class Prac4 { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a measurement in meters: ");
double meters = scan.nextDouble();
double inches = meters*39.37;
int feet =(int) inches/12;
double remain = inches % 12;
double finches = remain*12;
double miles = feet/1760.0;
System.out.println("Remain: " + remain);
System.out.println(meters +" meters is equivalent to "+ inches +" inches");
System.out.println(meters +" meters is equivalent to "+ feet +" feet and " + finches + " inches");
System.out.println(meters +" meters is equivalent to "+ miles +" miles");
} }
----------------------------------------------------------------------------------------------------------------------------
What happens when you run the program
Enter a measurement in meters: 10
Remain: 9.699999999999989
10.0 meters is equivalent to 393.7 inches
10.0 meters is equivalent to 32 feet and 116.39999999999986 inches
10.0 meters is equivalent to 0.01818181818181818 miles
What however my formula which was inches%12 wasn't giving me just the decimal. It gives me the decimal in inches. So using 10 meters converting it into inches I get 393.7 inches and then divided by 12 gives you 32.8 and some change. But instead of just giving me that .8 and change it was giving me .8x12 to get 9.6 and change.
It's what I want in the end but I'm completely confused on how the program is getting it when it shouldn't ( I think?)
My Code (I entered the remain line since I wanted to see the actual value.
--------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class Prac4 { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a measurement in meters: ");
double meters = scan.nextDouble();
double inches = meters*39.37;
int feet =(int) inches/12;
double remain = inches % 12;
double finches = remain*12;
double miles = feet/1760.0;
System.out.println("Remain: " + remain);
System.out.println(meters +" meters is equivalent to "+ inches +" inches");
System.out.println(meters +" meters is equivalent to "+ feet +" feet and " + finches + " inches");
System.out.println(meters +" meters is equivalent to "+ miles +" miles");
} }
----------------------------------------------------------------------------------------------------------------------------
What happens when you run the program
Enter a measurement in meters: 10
Remain: 9.699999999999989
10.0 meters is equivalent to 393.7 inches
10.0 meters is equivalent to 32 feet and 116.39999999999986 inches
10.0 meters is equivalent to 0.01818181818181818 miles