Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Question in numbering

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Question in numbering

    public class Area {
    	public static void main(String[] args) {
    		double radius;
    		double area;
    		radius = 10;
    		area = radius * radius * 3.1415 ;
    		System.out.println("The area of circle is " + area + ".");
    	}
    }
    I am a newbie here. LOL
    I would like to ask why the result comes:

    "The area of circle is 314.15000000000003." ???
    Last edited by copeg; September 18th, 2010 at 11:06 AM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question in numbering

    What output would you expect?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Question in numbering

    This is a normal thing. Your computer can only be so accurate. It is not a program problem, but rather that you computer has calculated it as best as possible. I don't really know how to explain it.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

  5. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    cppcppcpp (September 20th, 2010)

  6. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question in numbering

    Quote Originally Posted by copeg View Post
    What output would you expect?
    I just expect it was

    "The area of circle is 314.15." before

  7. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Question in numbering

    Now go through the page I linked and correct your expectations.

    db

  8. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question in numbering

    if you want to your answer to be displayed 314.15, you should put, "%2f, area" if im not mistaken its look like in the C programming it will just put 2 decimal place, i hope it can helps you.

  9. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question in numbering

    instead of this System.out.println("The area of circle is " + area + "."); use this System.out.printf("The area of cirlcis %3f" , area); so that the modulo will be read in the java compiler, with no problem

  10. #9
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question in numbering

    @sanz:
    you're right..it's like C progmramming...

    @cppcppcppcpp

    change the System.out.println("The area of circle is " + area + ".");
    to:
    System.out.printf("The area of cirle is %.2f" , area);

  11. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question in numbering

    Should be getting 314.15 for the area... I got it..
    Now get more fun with it and require input from user.

    import java.util.*;

    public class Area {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double area;
    System.out.println("Please enter a radius");
    double radius = input.nextDouble();
    area = radius * radius * 3.1415 ;
    System.out.println("The area of circle is " + area + ".");
    }
    }