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: Why is the som of this these two not correct

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why is the som of this these two not correct

    I have the code bellow and I get as output:
    30.299999999999997
    30

    Why is the output:30.299999999999997 and not 30.3?

    public class demo {
    	private static double som(double a, double b) {
    		return a+b;
    	}
     
    	private static int som(int a, int b) {
    		return a+b;
    	}
     
    	public static void main(String args[]) {
    		System.out.println(som(20.20,10.10));
    		System.out.println(som(20,10));
     
    	}
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Why is the som of this these two not correct

    Explanation here:

    https://docs.oracle.com/javase/tutor...datatypes.html

    The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Why is the som of this these two not correct

    Quote Originally Posted by John Joe View Post
    Explanation here:

    https://docs.oracle.com/javase/tutor...datatypes.html

    The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
    What should I use instead?

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Why is the som of this these two not correct

    You can use DecimalFormat .

     DecimalFormat value = new DecimalFormat("#.#");
     System.out.println(value.format(som(20.20, 10.10)));

    Don't forget to add this line

    import java.text.DecimalFormat;
    Whatever you are, be a good one

Similar Threads

  1. Is this the correct way?
    By Durabrite in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2014, 08:56 AM
  2. Need to know if the Answer is correct?
    By AeZee.Ess in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2014, 02:08 PM
  3. Please help me correct my program
    By Katt in forum Other Programming Languages
    Replies: 2
    Last Post: February 10th, 2013, 02:06 PM
  4. Replies: 15
    Last Post: November 4th, 2011, 03:52 AM
  5. Can anybody correct the code?
    By ur2cdanger in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2011, 07:07 PM