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 3 of 3

Thread: java -working with decimal numbers

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java -working with decimal numbers

    hello java forum,
    Good Morning.


    Here i came up with a mess about a program.

    Iam not obtaining a accurate result .

    But , in C the program is giving an accurate result.



    in java , the program goes like this

    public class FloatingNumber {
    public static void main(String...args) {
    double i;
    double num=0.0;
    for(i=0.0;i<10;i++)
    num=num+0.1;
    System.out.println(num);
    }
    }

    // output =0.9999999999999999

    // In C the program goes like this

    #include<stdio.h>
    #include<math.h>
    main()
    {
    double i, num=0.0;
    for(i=0;i<10;i++)
    num=num+0.1;
    printf("%f",num);
    }

    // output =1.00000

    please explain me the difference and resolve my problem.

    Thanks in advance


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java -working with decimal numbers

    Quote Originally Posted by raam View Post
    please explain me the difference and resolve my problem.
    Please, see this thread.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,402
    My Mood
    Mellow
    Thanks
    83
    Thanked 472 Times in 463 Posts

    Default Re: java -working with decimal numbers

    Don't post duplicates, and welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

Similar Threads

  1. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  2. [SOLVED] decimal format not working
    By lordofrandom in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 7th, 2013, 04:36 PM
  3. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  4. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  5. [SOLVED] Rounding Decimal Numbers Question
    By Nuggets in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 04:12 PM