Java Addition throws wrong Decimal Result

I have an error in Java for an Android app where two doubles have to be added together. The third addition then results in a long decimal number, e.g. 137.239999999. How can I bypass this error?

Addition

Declaration

Result

    public void calculateOnAction() {

    editString();
    sumscans ++;
    sumgrosweight = sumgrosweight + grosweight;
    sumnetweight = sumnetweight + netweight;
    String sumgroschar = Double.toString(sumgrosweight);
    String sumnetchar = Double.toString(sumnetweight);
    TextView textViewgrosweight = findViewById(R.id.textviewnetsum);
    textViewgrosweight.setTypeface(null, Typeface.BOLD);
    textViewgrosweight.setText(sumnetchar);
    textViewgrosweight.setTextSize(20);
    TextView textViewnetweight = findViewById(R.id.textviewgrossum);
    textViewnetweight.setTypeface(null, Typeface.BOLD);
    textViewnetweight.setText(sumgroschar);
    textViewnetweight.setTextSize(20);
    TextView textViewsumscans = findViewById(R.id.textviewscansum);
    textViewsumscans.setTypeface(null, Typeface.BOLD);
    textViewsumscans.setTextSize(20);
    textViewsumscans.setText(Integer.toString(sumscans));
}

// The function that splits the input string into two strings and afterwards converts it to two float
public void editString() {

    String replacedString0;
    String replacedString1;
    EditText barcodefield = findViewById(R.id.barcode);
    barcodefield.requestFocus();

            cacheBarcode = barcodefield.getText().toString().split(";");
            if (cacheBarcode[0].contains(",") || cacheBarcode[1].contains(",")) {
                replacedString0 = cacheBarcode[0].replace(",", ".");
                replacedString1 = cacheBarcode[1].replace(",", ".");
                grosweight = Double.parseDouble(replacedString0);
                netweight = Double.parseDouble(replacedString1);

            } else {
                grosweight = Double.parseDouble(cacheBarcode[0]);
                netweight = Double.parseDouble(cacheBarcode[1]);
            }

            barcodefield.setText(null);
            barcodefield.requestFocus();
            Arrays.fill(cacheBarcode, null);
        }

>Solution :

Using float and double types can generate rounding problems also for very simple operations.

As an example the following code

System.out.println(0.7+0.1);

prints 0.7999999999999999 and not 0.8

To solve those kinds of problems java offer a class named BigDecimal that can be used to do exactly that:

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale).

Basically with a BigDecimal is the programmer that decides the number of digits after the comma and in the case of operations that increase the number of digits is always the programmer that decides how to round the result .

In poarticular in your code you need to create the BigDecimal and sum them using add:

Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()).

In this case the following code

Bigdecimal first = new BigDecimal("0.7");
Bigdecimal second = new BigDecimal("0.1");
Bigdecimal result = first.add(second);
System.out.println(result);

will print 0.8

Leave a Reply