java - how to display BigDecimal properly as a string -


i trying learn java android programming
practice trying write calculator program basic arithmetic operations , decimal point button
tried using primitive numerical types such int , double, if use double variable type, number such 2.111 gets displayed 2.11099999, other seemed work fine (my question given here)
suggested in answer other question modified program use bigdecimal data type instead of double, when click on 1 button, mantissa get's displayed 1.0, instead of 1 in previous programs had used double
how can fix this?

code 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 menu; adds items action bar if 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{         // should mean opr1 has value in         // add current mantissa value 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{         // should mean opr1 has value in         // add current mantissa value 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{         // should mean opr1 has value in         // add current mantissa value 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{         // should mean opr1 has value in         // add current mantissa value 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 this,

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

it displays 2.111 instead of 2.1109999. why that?

------ edit 2 ------

i have made changes logic of program, works on strings perform related display, while uses double.parsedouble() function convert mantissa string value double perform related calculations. updated code given below if comment on

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 menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      public void updatemantissa(){         // version should called num pad buttons, 0 - 9         updatemantissa(true);     }     public void updatemantissa(boolean frmnumpad){         // version should called other buttons besides 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{             // should mean opr1 has value in             // add current mantissa value 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{             // should mean opr1 has value in             // add current mantissa value 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{             // should mean opr1 has value in             // add current mantissa value 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{             // should mean opr1 has value in             // add current mantissa value 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();     }  } 

by reading question linked to, not clear if concern floating precision issue or floating printing issue. people in original question advised in favour of bigdecimal have feeling original concern has latter - i.e. printing value - , still facing.

it -almost- never idea print double value using string.valueof(). happens behind scenes conversion takes place convert floating point value string based οn default locale. instead of leaving system make conversion should perform conversion yourself.

the way decimalformat class. example code:

double mantissa_value = 2.11099999;  decimalformat df = new decimalformat("0.###"); // un-comment line below take english locale account when converting //df.setdecimalformatsymbols(new decimalformatsymbols(locale.english));  // print '2.11099999' in original case system.out.println( string.valueof(mantissa_value) );   // print '2.111' or '2,111' depending on locale. if uncomment line above english locale print '2.111' system.out.println( df.format(mantissa_value) );  

the above decimal format print double value 1, 2, or 3 digits after decimal. in case need variable precision can keep counter in variable user enters characters , use counter produce decimalformat desired decimal digits. same approach can followed bigdecimal case. hope helps


Comments