2012-03-01 49 views
1

在我的應用程序我顯示數字字符串,並注意到「7.000444」顯示爲「7.00044」。另外,「0.567567」顯示爲「0.56757」。最後,添加任何非數字部分(即字母)使其顯示全部數字,然後顯示另一個鍵。有沒有辦法阻止前兩者的行爲?我通過將它添加到數據庫然後顯示在ListView中來顯示它們。創建字符串的代碼是一組按鈕,對於按鈕0-9,它是:顯示時應該如何停止舍入數字的字符串?

Button one; 
one = (Button)findViewById(R.id.one); 
one.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      working = BigDecimal.valueOf(1); 
      now = working.toString(); 
      total1 = total.concat(now); 
      equation1 = equation.concat(now); 
      equation = equation1; 
      total = total1; 
      if(b){mnDbHelper.updateNote(equation, mnDbHelper.testCount());} 
      else{mnDbHelper.createNote(equation); 
      b=true;} 
      fillData(); 
     } 
}); 

隨着1被更改爲必要。 小數點按鈕如下:

Button decimal; 
decimal = (Button)findViewById(R.id.decimal); 
decimal.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      now = "."; 
      equation1 = equation.concat(now); 
      total1 = total.concat(now); 
      equation = equation1; 
      total = total1; 
      if(b){mnDbHelper.updateNote(equation, mnDbHelper.testCount());} 
      else{mnDbHelper.createNote(equation); 
      b=true;} 
      fillData(); 
     } 

});

這裏是FillData的代碼:

private void fillData() { 
    // Get all of the notes from the database and create the item list 
    Cursor c = mnDbHelper.fetchAllNotes(); 

    startManagingCursor(c); 

    String[] columns = new String[] {EquationsDbAdapter.KEY_VALUE}; 

    int to[] = new int[] {android.R.id.text1}; 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1 ,c , columns , to); 

    setListAdapter(adapter); 

} 

下面是其他按鈕中的一個的示例代碼:

Button times; 
     times = (Button)findViewById(R.id.times); 
     times.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       now = "*"; 
       equation1 = equation.concat(now);r = equation; 
       equation = equation1;z = equation; 
       if(total!="") 
       {try{ 
        formatter.parse(total); 
       } 
       catch(Throwable t){ 
        really = false; 
       } 
       if(really){ 
        try { 
         mDbHelper.createNote((BigDecimal) formatter.parse(total), fa, fa, fa, fa, fa, fa, jkl, fa, fa); 
        } catch (ParseException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       total = ""; 
       total1 = "";} 
       else{really = true;}} 
       mDbHelper.createNote(jkl, fa, fa, t, fa, fa, fa, jkl, fa, fa); 
       if(b){mnDbHelper.updateNote(equation, mnDbHelper.testCount());} 
       else{mnDbHelper.createNote(equation); 
       b=true;} 
       fillData(); 
      } 
    }); 

格式化如下:

DecimalFormat formatter = new DecimalFormat("##################################################################################.################################"); 

編輯:奇怪的是,「0.33333333」顯示不顯示正確,但是當添加*時,確實如此,但是當從中減去*時它再次沒有。 一如既往,非常感謝幫助。

+2

你必須對你的字符串做些奇怪的事情。你如何顯示它? System.out.println()不會修剪字符串,無論它們的內容如何。 – kba 2012-03-01 22:44:09

+3

你究竟如何顯示數字(或將它們轉換爲字符串)? – Taymon 2012-03-01 22:46:14

+0

我通過將它添加到數據庫然後顯示在一個ListView中,將它添加到OP中來顯示它們。 – jersam515 2012-03-01 22:50:12

回答

0

我最終做的只是在字符串的開頭添加一個空格,並且工作。

1

編輯:

呵呵。那麼這個問題似乎不在formatter對象中。它會返回Double,除非已調用setParseBigDecimal(true),但如果您試圖將Double投射到BigDecimal,則會出現轉換錯誤。我不認爲你需要格式化,應調用以下,但我看不出導致截斷:

new BigDecimal(total) 

是犯罪嫌疑人在某個地方你是從Stringdouble去或float這是截斷BigDecimal的精度。例如,你確定你是不是做:

working = BigDecimal.valueOf((double)value); 

確保您使用的是String構造函數BigDecimal

working = new BigDecimal(valueStr); 

這確實字符串的字符一個字符處理以創建精確度沒有損失的BigDecimal

如果您提供更多詳細信息(如許多評論所要求的)並顯示實際創建BigDecimal的代碼,那麼我將編輯我的答案以提供更多信息。


代碼:

if(total!="") 

是不對的。您很少想要將字符串與==!=進行比較。我懷疑應該是:

if (!("".equals(total))) 

也許

if (total.length() > 0) 
+0

我不顯示工作,我顯示從未使用double或float來構造自己的方程。 – jersam515 2012-03-01 23:25:07

+0

請編輯您的問題以顯示代碼@ jersam515。 – Gray 2012-03-01 23:26:11

+0

好的,我做到了。感謝您查看我的問題。 – jersam515 2012-03-01 23:29:12

1

這是非常有趣的,因爲很明顯,double代替BigDecimal,情況並非如此。 您提到了數據庫。也許你從數據庫中重新加載字段,並且記錄字段是DOUBLE而不是DECIMALDECIMAL,並且有錯誤的精度

+0

數據庫將字符串保存爲字符串。另一個計算使用blob。我覺得奇怪的是,「0.6758431」無法正確顯示,但在添加*之後,它顯示爲「0.6758431 *」 – jersam515 2012-03-02 00:09:01

相關問題