2014-02-21 23 views
-2

在此代碼文本視圖中的每個整數和十進制數字,如果我給文本框中的文本視圖輸入顯示與.0如何刪除該如何刪除.0當我給整數輸入

public class MainActivity extends Activity implements OnClickListener { 

     EditText etNum1; 
     EditText etNum2; 

     Button btnAdd; 
     Button btnSub; 
     Button btnMult; 
     Button btnDiv; 

     TextView tvResult; 

     String oper = ""; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // find the elements 
     etNum1 = (EditText) findViewById(R.id.etNum1); 
     etNum2 = (EditText) findViewById(R.id.etNum2); 

     btnAdd = (Button) findViewById(R.id.btnAdd); 
     btnSub = (Button) findViewById(R.id.btnSub); 
     btnMult = (Button) findViewById(R.id.btnMult); 
     btnDiv = (Button) findViewById(R.id.btnDiv); 

     tvResult = (TextView) findViewById(R.id.tvResult); 

     // set a listener 
     btnAdd.setOnClickListener((OnClickListener) this); 
     btnSub.setOnClickListener(this); 
     btnMult.setOnClickListener(this); 
     btnDiv.setOnClickListener(this); 

     } 
    @Override 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 
     double num1=0; 
     double num2=0; 
     double result=0; 


     // check if the fields are empty 
     if (TextUtils.isEmpty(etNum1.getText().toString()) 
      || TextUtils.isEmpty(etNum2.getText().toString())) { 
     return; 
     } 

     // read EditText and fill variables with numbers 

     num1 = Double.parseDouble(etNum1.getText().toString()); 
     num2 = Double.parseDouble(etNum2.getText().toString()); 



     // defines the button that has been clicked and performs the corresponding operation 
     // write operation into oper, we will use it later for output 
     switch (v.getId()) { 
     case R.id.btnAdd: 
      oper = "+"; 

     result = num1 + num2; 

      break; 
     case R.id.btnSub: 
      oper = "-"; 
      result = num1 - num2; 

      break; 
     case R.id.btnMult: 
      oper = "*"; 
      result = num1 * num2; 

      break; 
     case R.id.btnDiv: 
      oper = "/"; 
     result = num1/num2; 

      break; 
     default: 
      break; 
     } 

     // form the output line 


     String text=String.valueOf(result); 
     String str[]= text.split("."); 

     tvResult.setText(num1 + " " + oper + " " + num2 + " = " + str[1]); 

     } 
    } 
+4

@FD_更換您的最後一個行

String text=String.valueOf(result); String str[]= text.split("."); tvResult.setText(num1 + " " + oper + " " + num2 + " = " + str[1]); 

- 只需編輯問題! –

+0

@PreetSangha對不起,下次再來。 –

+0

不好意思將小數點分開。(點) –

回答

3

如果你不需要小數,只是用

Integer.parseInt() 

在你的代碼。

否則,您可以使用String.format()格式化字符串顯示0的小數位數:

String.format("%.0d", yourDouble) 
+0

+1謝謝@FD_對我來說這是一個有用的正則表達式,儘管......長時間囉嗦,噓聲問道,你的回答很有幫助,謝謝! – RossC

+1

@RossC作爲參考,這是[格式字符串](http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax),而不是正則表達式:p – thegrinner

+0

@thegrinner謝謝你,我從未見過它,非常感謝! – RossC

0

只需使用

tvResult.setText(num1 + " " + oper + " " + num2 + " = " + Integer.parseInt(result));