在我的應用程序我顯示數字字符串,並注意到「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」顯示不顯示正確,但是當添加*時,確實如此,但是當從中減去*時它再次沒有。 一如既往,非常感謝幫助。
你必須對你的字符串做些奇怪的事情。你如何顯示它? System.out.println()不會修剪字符串,無論它們的內容如何。 – kba 2012-03-01 22:44:09
你究竟如何顯示數字(或將它們轉換爲字符串)? – Taymon 2012-03-01 22:46:14
我通過將它添加到數據庫然後顯示在一個ListView中,將它添加到OP中來顯示它們。 – jersam515 2012-03-01 22:50:12