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: What's wrong with the calculation?

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default What's wrong with the calculation?

    A simple addition in Java:
    Float Value1 = 4.7, Value2 = 7.6
    Addition Result = Value1 + Value2 = 12.299999 not 12.3 ?
    Can someone help me to solve the problem without rounding the result?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with the calculation?

    Can you post the code that is the problem?

    Do you understand about using floating point numbers and how they are stored in a computer's memory?
    http://docs.oracle.com/cd/E19957-01/..._goldberg.html

    To get your desired results use the BigDecimal class with String values in its constructor:
          BigDecimal value1 = new BigDecimal("4.7");
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What's wrong with the calculation?

    Quote Originally Posted by Norm View Post
    Can you post the code that is the problem?
    Float Value1, Value2;
    .
    .
    .
    btn_Result.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Value2 = Float.parseFloat(edt.getText().toString());

    if (AddFlag == true){


    edt.setText(Value1 + Value2 +"");
    AddFlag=false;
    }
    if (SubFlag == true){

    edt.setText(Value1 - Value2 +"");
    SubFlag=false;
    }
    if (MulFlag == true){

    edt.setText(Value1 * Value2 +"");
    MulFlag=false;
    }
    if (DivFlag == true){

    edt.setText(Value1 / Value2 +"");
    DivFlag=false;
    }

    }
    });

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with the calculation?

    Did you try using the BigDecimal class?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: July 26th, 2014, 04:38 AM
  2. Replies: 2
    Last Post: February 28th, 2014, 09:39 AM
  3. BMI calculation
    By dantheman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 28th, 2012, 09:57 PM
  4. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM
  5. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM