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?
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.
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?
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.
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;
}
}
});
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.