0

I am trying to learn java to get into android programming
For my practice I am trying to write a calculator program with basic arithmetic operations and a decimal point button
I tried to do it using primitive numerical types such as int and double, but if I use double as a variable type, a number such as 2.111 gets displayed as 2.11099999, but other than that it seemed to work fine (my question about this is given here)
As suggested in the answer to my other question I modified my program to use BigDecimal data type instead of double, but now when I click on the 1 button, the mantissa get's displayed as 1.0, instead of just 1 as in the previous programs where I had used double
How can I fix this?

Code to update mantissa:

public void updateMantissa(){

    // mantissa_valueBD = BigDecimal.valueOf(mantissa_value);

    if(mantissa_value == BigDecimal.ZERO){
        mantissa_str = "0";
    }
    /*
    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    */

    // /*
    else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
        // mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_value.toString();
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 4 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    // */
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    thisText.setText(mantissa_str);
}

Full code (MainActivity class):

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import static java.lang.Math.pow;
// import static java.lang.Math.ceil;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class MainActivity extends Activity {
// private double mantissa_value = 0;
// private BigDecimal mantissa_value = new BigDecimal(0);
// private BigDecimal opr1BD, opr2BD, resBD, mantissa_valueBD;
/*
private double opr1 = 0;
private double opr2 = 0;
private double res = 0;
// */

BigDecimal opr1 = BigDecimal.ZERO;
BigDecimal opr2 = BigDecimal.ZERO;
BigDecimal res = BigDecimal.ZERO;
BigDecimal tempBD = BigDecimal.ZERO;
BigDecimal mantissa_value = BigDecimal.ZERO;

private enum OprTypes {NONE, PLUS, MINUS, MULT, DIV}
OprTypes oprLatest = OprTypes.NONE;
private boolean oprPlusClicked = false;
private boolean oprMinusClicked = false;
private boolean oprMultClicked = false;
private boolean oprDivClicked = false;
private boolean decimalClicked = false;
private int numOfClicksAfterDecimal = 0;
private String mantissa_str;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updateMantissa();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void updateMantissa(){

    // mantissa_valueBD = BigDecimal.valueOf(mantissa_value);

    if(mantissa_value == BigDecimal.ZERO){
        mantissa_str = "0";
    }
    /*
    else if(ceil(mantissa_value) == mantissa_value){
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_valueBD);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    */

    // /*
    else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
        // mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_value.toString();
        mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 4 ); 
    }

    else{
        mantissa_str = String.valueOf(mantissa_value);
        mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
    }
    // */
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    thisText.setText(mantissa_str);
}

public void onClkBn0(View view){
    TextView thisText = (TextView) findViewById(R.id.mantissa);
    if(decimalClicked == false){
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = mantissa_value.add(BigDecimal.valueOf(0.0));
        updateMantissa();
    }
    else{
        if(mantissa_value == BigDecimal.ZERO){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        else if(mantissa_value.setScale(0, RoundingMode.CEILING)== mantissa_value){
            if(numOfClicksAfterDecimal == 1){
                mantissa_str = mantissa_str + ".0";
            }
            else{
                mantissa_str = mantissa_str + "0";
            }
        }

        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*0;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*0);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
        thisText.setText(mantissa_str);
    }
}

public void onClkBn1(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+1;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(1.0));
    }

    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn2(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+2;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(2.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*2;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*2);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn3(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+3;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(3.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*3;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*3);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn4(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+4;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(4.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*4;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*4);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn5(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+5;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(5.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*5;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*5);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn6(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+6;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(6.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*6;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*6);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn7(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+7;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(7.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*7;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*7);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn8(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+8;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(8.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*8;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*8);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBn9(View view){
    if(decimalClicked == false){
        // mantissa_value = mantissa_value*10+9;
        tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0));
        mantissa_value = tempBD.add(BigDecimal.valueOf(9.0));
    }
    else{
        // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*9;
        tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*9);
        mantissa_value = mantissa_value.add(tempBD);
        numOfClicksAfterDecimal++;
    }
    updateMantissa();
}

public void onClkBnDot(View view){
    decimalClicked = true;
}

public void onClkBnCan(View view){
    mantissa_value = BigDecimal.ZERO;
    opr1 = BigDecimal.ZERO;
    opr2 = BigDecimal.ZERO;
    res = BigDecimal.ZERO;
    /*
    opr1 = 0;
    opr2 = 0;
    res = 0;
    // */
    oprLatest = OprTypes.NONE;
    oprPlusClicked = false;
    oprMinusClicked = false;
    oprMultClicked = false;
    oprDivClicked = false;
    decimalClicked = false;
    numOfClicksAfterDecimal = 1;
}


public void onClkBnPlus(View view){
    if(oprPlusClicked == false){
        oprPlusClicked = true;
        opr1 = mantissa_value;
        // mantissa_value = 0;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 + mantissa_value;
        opr1 = opr1.add(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    oprLatest = OprTypes.PLUS;
}


public void onClkBnMinus(View view){
    if(oprMinusClicked == false){
        oprMinusClicked = true;
        opr1 = mantissa_value;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 - mantissa_value;
        opr1 = opr1.subtract(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    oprLatest = OprTypes.MINUS;
}

public void onClkBnMult(View view){
    if(oprMultClicked == false){
        oprMultClicked = true;
        opr1 = mantissa_value;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 * mantissa_value;
        opr1 = opr1.multiply(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    oprLatest = OprTypes.MULT;
}

public void onClkBnDiv(View view){
    if(oprDivClicked == false){
        oprDivClicked = true;
        opr1 = mantissa_value;
        mantissa_value = BigDecimal.ZERO;
    }
    else{
        // this should mean opr1 already has some value in it
        // so add the current mantissa value to opr1
        // opr1 = opr1 / mantissa_value;
        opr1 = opr1.divide(mantissa_value);
        mantissa_value = BigDecimal.ZERO;
    }
    oprLatest = OprTypes.DIV;
}


public void onClkBnRes(View view){
    if(oprPlusClicked == true || oprMinusClicked == true || oprMultClicked == true || oprDivClicked == true){
        switch(oprLatest){
        case PLUS:
            // mantissa_value = opr1 + mantissa_value;
            mantissa_value = opr1.add(mantissa_value);
            break;
        case MINUS:
            // mantissa_value = opr1 - mantissa_value;
            mantissa_value = opr1.subtract(mantissa_value);
            break;
        case MULT:
            // mantissa_value = opr1 * mantissa_value;
            mantissa_value = opr1.multiply(mantissa_value);
            break;
        case DIV:
            // mantissa_value = opr1 / mantissa_value;
            mantissa_value = opr1.divide(mantissa_value);
            break;
        case NONE:
            break;
        }
    }
    oprLatest = OprTypes.NONE;
    oprPlusClicked = false;
    oprMinusClicked = false;
    oprMultClicked = false;
    oprDivClicked = false;
    updateMantissa();
}
 }

------ EDIT ------
If I do something like this,

public static void main(String[] args){
    // double a;
    a = 2.111;
    System.out.println(a);
}

it displays exactly 2.111 instead of 2.1109999. Why is that?

------ EDIT 2 ------

I have made changes to the logic of my program, so that it now works on strings to perform anything related to display, while uses the Double.parseDouble() function to convert the mantissa string value into double to perform anything related to calculations. The updated code is given below if anyone would like to comment on it

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import static java.lang.Math.pow;
import static java.lang.Math.ceil;
// import java.math.BigDecimal;
import java.text.NumberFormat;

public class MainActivity extends Activity {

     private double mantissa_value = 0;
    // private BigDecimal mantissa_value = new BigDecimal(0);
    // private BigDecimal opr1BD, opr2BD, resBD, mantissa_valueBD;
    // /*
    private double opr1 = 0;
    // */
    NumberFormat number =  NumberFormat.getNumberInstance();

    private enum OprTypes {NONE, PLUS, MINUS, MULT, DIV}
    OprTypes oprLatest = OprTypes.NONE;
    private boolean oprPlusClicked = false;
    private boolean oprMinusClicked = false;
    private boolean oprMultClicked = false;
    private boolean oprDivClicked = false;
    private boolean decimalClicked = false;
    private boolean resetMantissa = true;
    private String mantissa_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // updateMantissa();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void updateMantissa(){
        // This is the version that should get called from the num pad buttons, 0 - 9
        updateMantissa(true);
    }
    public void updateMantissa(boolean frmNumPad){
        // this is the version that should get called from other buttons besides the num pad buttons
        if(frmNumPad != true){
            long mantissa_val_int_part = (long)mantissa_value;
            if(mantissa_value == (double)mantissa_val_int_part){
                mantissa_str = mantissa_str.substring(0, mantissa_str.length() - 2 );
            }
            else if (mantissa_str.length() > 10){
                mantissa_str = mantissa_str.substring(0, Math.min(mantissa_str.length(), 10));
            }

        }


        TextView thisText = (TextView) findViewById(R.id.mantissa);
        thisText.setText(mantissa_str);
    }

    public void onClkBn0(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }

        if(decimalClicked == false){
            if( !(Double.parseDouble(mantissa_str)==0.0) ) 
                mantissa_str = mantissa_str + "0";
        }
        else{
            mantissa_str = mantissa_str + "0";
        }
        TextView thisText = (TextView) findViewById(R.id.mantissa);
        thisText.setText(mantissa_str);
    }

    public void onClkBn1(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "1";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn2(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "2";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn3(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "3";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn4(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "4";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn5(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "5";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn6(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "6";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn7(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "7";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn8(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "8";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBn9(View view){
        if (resetMantissa == true){
            mantissa_str = "";
            resetMantissa = false;
        }
        mantissa_str = mantissa_str + "9";
        mantissa_value = Double.parseDouble(mantissa_str);
        updateMantissa();
    }

    public void onClkBnDot(View view){
        if(decimalClicked == false){
            decimalClicked = true;
            mantissa_str = mantissa_str + ".";
        }
    }

    public void onClkBnCan(View view){
        mantissa_value = 0;
        // /*
        opr1 = 0;
        // */
        oprLatest = OprTypes.NONE;
        oprPlusClicked = false;
        oprMinusClicked = false;
        oprMultClicked = false;
        oprDivClicked = false;
        decimalClicked = false;
        mantissa_value = 0;
        opr1 = 0;
        resetMantissa = true;
        updateMantissa(false);
    }


    public void onClkBnPlus(View view){
        if(oprPlusClicked == false){
            oprPlusClicked = true;
            decimalClicked = false;
            opr1 = Double.parseDouble(mantissa_str);
            mantissa_value = 0;
        }
        else{
            // this should mean opr1 already has some value in it
            // so add the current mantissa value to opr1
            mantissa_value = Double.parseDouble(mantissa_str);
            opr1 = opr1 + mantissa_value;
            mantissa_value = opr1;
            decimalClicked = false;
            mantissa_str = Double.toString(mantissa_value);
            updateMantissa(false);
        }
        oprLatest = OprTypes.PLUS;
        resetMantissa = true;
    }


    public void onClkBnMinus(View view){
        if(oprMinusClicked == false){
            oprMinusClicked = true;
            decimalClicked = false;
            opr1 = Double.parseDouble(mantissa_str);
            mantissa_value = 0;
        }
        else{
            // this should mean opr1 already has some value in it
            // so add the current mantissa value to opr1
            mantissa_value = Double.parseDouble(mantissa_str);
            opr1 = opr1 - mantissa_value;
            mantissa_value = opr1;
            decimalClicked = false;
            mantissa_str = Double.toString(mantissa_value);
            updateMantissa(false);
        }
        oprLatest = OprTypes.MINUS;
        resetMantissa = true;
    }

    public void onClkBnMult(View view){
        if(oprMultClicked == false){
            oprMultClicked = true;
            decimalClicked = false;
            opr1 = Double.parseDouble(mantissa_str);
            mantissa_value = 0;
        }
        else{
            // this should mean opr1 already has some value in it
            // so add the current mantissa value to opr1
            mantissa_value = Double.parseDouble(mantissa_str);
            opr1 = opr1 * mantissa_value;
            mantissa_value = opr1;
            decimalClicked = false;
            mantissa_str = Double.toString(mantissa_value);
            updateMantissa(false);
        }
        oprLatest = OprTypes.MULT;
        resetMantissa = true;
    }

    public void onClkBnDiv(View view){
        if(oprDivClicked == false){
            oprDivClicked = true;
            decimalClicked = false;
            opr1 = Double.parseDouble(mantissa_str);
            mantissa_value = 0;
        }
        else{
            // this should mean opr1 already has some value in it
            // so add the current mantissa value to opr1
            mantissa_value = Double.parseDouble(mantissa_str);
            opr1 = opr1 / mantissa_value;
            mantissa_value = opr1;
            decimalClicked = false;
            mantissa_str = Double.toString(mantissa_value);
            updateMantissa(false);
        }
        oprLatest = OprTypes.DIV;
        resetMantissa = true;
    }


    public void onClkBnRes(View view){
        mantissa_value = Double.parseDouble(mantissa_str);
        if(oprPlusClicked == true || oprMinusClicked == true || oprMultClicked == true || oprDivClicked == true){
            switch(oprLatest){
            case PLUS:
                mantissa_value = opr1 + mantissa_value;
                break;
            case MINUS:
                mantissa_value = opr1 - mantissa_value;
                break;
            case MULT:
                mantissa_value = opr1 * mantissa_value;
                break;
            case DIV:
                mantissa_value = opr1 / mantissa_value;
                break;
            case NONE:
                break;
            }
        }
        oprLatest = OprTypes.NONE;
        oprPlusClicked = false;
        oprMinusClicked = false;
        oprMultClicked = false;
        oprDivClicked = false;
        decimalClicked = false;
        mantissa_str = Double.toString(mantissa_value);
        updateMantissa(false);
    }

    public void onClkBnOff(View view){
        finish();
    }

}
Community
  • 1
  • 1
user13267
  • 6,871
  • 28
  • 80
  • 138

2 Answers2

1

Basically it displays the Double that way only. The following small code addition before displaying the number should help.

BigDecimal chk=BigDecimal.valueOf(val);
BigDecimal chk3=chk.subtract(BigDecimal.valueOf(chk.intValue()));
if(chk3.compareTo(BigDecimal.ZERO)==0)
    System.out.print(BigDecimal.valueOf(chk.intValue()));
else
    System.out.print(chk);

chk is the value you want to display. Just add chk3 which is just the int value of it. This code prints 50.1 for 50.1 and 50 for 50.00. Hope this helps.

--EDIT--

I found another mistake in your code. You are doing:

tempBD = mantissa_value.multiply(BigDecimal.valueOf(10.0)); This is wrong as the nearest value to 10.0 is stored, but you want exactly 10.0, then you should do:

tempBD = mantissa_value.multiply(BigDecimal.valueOf("10.0"));

So modified code for void onClkBn1(View view) is the following:

public void onClkBn1(View view){
if(decimalClicked == false){
    // mantissa_value = mantissa_value*10+1;
    tempBD = mantissa_value.multiply(BigDecimal.valueOf("10.0"));
    mantissa_value = tempBD.add(BigDecimal.valueOf("1.0"));
}

else{
    // mantissa_value = mantissa_value + pow(10, -numOfClicksAfterDecimal)*1;
    tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1);
    mantissa_value = mantissa_value.add(tempBD);
    numOfClicksAfterDecimal++;
}
updateMantissa();
}

And I am not sure how you are using tempBD = BigDecimal.valueOf(pow(10, -numOfClicksAfterDecimal)*1); shouldn't it be Math.pow(double, double). So check that too please.

Ankit Aggarwal
  • 2,367
  • 24
  • 30
  • please upvote if it works, else tell me what problem still persists :) – Ankit Aggarwal Jul 15 '13 at 11:20
  • Thanks for the suggestion, but I have converted all my variables to BigDecimal, so BigDecimal.valueOf() does not work anymore, and besides, if I understood this code snippet correctly, it will solve the 1 appearing as 1.0 problem, but probably not the 2.111 appearing as 2.11099999 problem. Any other suggestions? – user13267 Jul 16 '13 at 00:32
  • But if you use BigDecimal, the 2.111 appearing as 2.1109999 should never occur. If it occurs then you are using BigDecimal wrong. I found your mistake, see my editted answer. It should run fine now :) – Ankit Aggarwal Jul 16 '13 at 07:27
  • Thank you for your help. One thing about the big decimal though, the valueOf() has a version that takes double as argument as well, so why does it have to be string? In any case I have made drastic changes to my program since I last updated here, and I have got around my problem by using double values and the Double.parseDouble() function to convert string to doubles. But since I am using doubles to store the result of calculation, if I want to extend the calculator to use larger numbers in the future I might have some problem, in which case I will probably need to go back to BigDecimal. – user13267 Jul 16 '13 at 08:28
  • so I will keep your code corrections in my backup, and I will also update my post with the latest version of my code if you would like to have a look at it – user13267 Jul 16 '13 at 08:28
  • The change I am suggesting is minimal. valueOf() has string version too which will work as intended with huge numbers too. Also if it is solved, please mark this as answered then. – Ankit Aggarwal Jul 16 '13 at 08:31
1

By also reading the question you linked to, it is not clear if your concern is a floating precision issue or a floating printing issue. People in the original question advised in favour of BigDecimal but I have the feeling that your original concern has to do with the latter - i.e. printing the value - and this is what you are still facing.

It is -almost- never a good idea to print a double value using String.valueOf(). What happens behind the scenes is that a conversion takes place to convert the floating point value to a string based οn your default locale. Instead of leaving the system to make that conversion you should perform the conversion yourself.

The way to do that is the DecimalFormat class. Example code:

double mantissa_value = 2.11099999;

DecimalFormat df = new DecimalFormat("0.###");
// Un-comment the line below to take English locale into account when converting
//df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));

// will print '2.11099999' this is what you get in your original case
System.out.println( String.valueOf(mantissa_value) ); 

// will print '2.111' or '2,111' depending on your locale. if you uncomment the line above for English locale it will print '2.111'
System.out.println( df.format(mantissa_value) ); 

The above decimal format will print the double value with 1, 2, or 3 digits after the decimal. In case you need a variable precision you can keep a counter in a variable as the user enters the characters and then use that counter to produce a DecimalFormat with the desired decimal digits. The same approach can be followed for the BigDecimal case. I hope that helps

c.s.
  • 4,786
  • 18
  • 32
  • yes my problem is mainly related to displaying the string, at least for now, until I decide to make a calculator with a large precision. If I use DecimalFormat, it looks like I won't even have to use BigDecimal, as I can just work with double values. However I cannot find a way to change the format o the DecimalFormat object obce it is created (you're example shows a constructor, but I need to change the format continuously, as the user types in numbers). – user13267 Jul 16 '13 at 01:42
  • Keeping that problem aside, how do I make a DecimalFormat keep the integer parts of a number as it is, while only specifying the number of digits in the fractional part? I ask this since it looks like you always have to specify the number of digits in the whole number as well as the fractional part – user13267 Jul 16 '13 at 01:43