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?




    public void calculateOnAction() {

    sumscans ++;
    sumgrosweight = sumgrosweight + grosweight;
    sumnetweight = sumnetweight + netweight;
    String sumgroschar = Double.toString(sumgrosweight);
    String sumnetchar = Double.toString(sumnetweight);
    TextView textViewgrosweight = findViewById(;
    textViewgrosweight.setTypeface(null, Typeface.BOLD);
    TextView textViewnetweight = findViewById(;
    textViewnetweight.setTypeface(null, Typeface.BOLD);
    TextView textViewsumscans = findViewById(;
    textViewsumscans.setTypeface(null, Typeface.BOLD);

// 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(;

            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]);

            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


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);

will print 0.8

Leave a Reply