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

Thread: working with decimal point numbers

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

    Smile working with decimal point numbers

    Good Morning,
    please suggest me a better code .

    // my java 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


    // The same program in C 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.0000


    /** please do explain me why java is not producing an accurate value as C program did.
    For an accurate result , please do sugges me good code for java
    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: working with decimal point numbers

    You can use math.round to round to 1.0

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

    Default Re: working with decimal point numbers

    Quote Originally Posted by raam View Post
    /** please do explain me why java is not producing an accurate value as C program did.
    Floating-point numbers have these problems because they are stored on a finite number of bits. This happens in Java, in C, in C#, in Javascript, etc...
    Floating-point numbers cannot accurately represent all numbers. For example 0.3 cannot be accurately represented. It will be very very close to 0.3 but never exactly 0.3. Where you can "see" a difference is when there is an implicit or explicit rounding, especially when you use some "print" method.
    You should understand this before criticizing your or other code.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: working with decimal point numbers

    Read this article for further information: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Similar Threads

  1. java -working with decimal numbers
    By raam in forum Member Introductions
    Replies: 2
    Last Post: December 9th, 2013, 05:44 AM
  2. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  3. no ending after decimal point.
    By miller4103 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 10th, 2013, 04:29 PM
  4. Fixed Point Numbers - Mathematics
    By BobDole6395 in forum Object Oriented Programming
    Replies: 2
    Last Post: July 11th, 2012, 05:51 PM
  5. [SOLVED] Rounding Decimal Numbers Question
    By Nuggets in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 04:12 PM