Results 1 to 3 of 3
  1. #1
    calistoknight is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Question about how Modulus works

    Hi 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
    Last edited by calistoknight; 09-24-2012 at 02:40 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Question about how Modulus works

    Please go thorough BB Code List - Java Programming Forum and edit your post accordingly.

    A question for you: As per your understanding, how much is 10 % 4?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Question about how Modulus works

    Cross posted and answered, not that calistoknight would be courteous enough to tlel us about that.
    https://forums.oracle.com/forums/thr...readID=2443140

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How this works
    By snowed in forum New To Java
    Replies: 2
    Last Post: 04-02-2012, 02:08 AM
  2. modulus value keeps returning a zero value
    By leoshiner in forum New To Java
    Replies: 3
    Last Post: 10-24-2010, 04:11 PM
  3. Tell me how this loop works.
    By hydride in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 04:50 AM
  4. String.contains works in jdk 6, not in 1.4.2
    By ScottThornley in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 03:04 PM
  5. [SOLVED] Modulus/Remainder results
    By antgaudi in forum New To Java
    Replies: 9
    Last Post: 10-13-2008, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •