【发布时间】:2012-03-01 22:42:25
【问题描述】:
在我的应用程序中,我正在显示数字字符串,并注意到“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() 不会修剪字符串,无论其内容如何。
-
您如何准确地显示数字(或将它们转换为字符串)?
-
我通过将其添加到数据库中来显示它们,然后将其显示在 ListView 中,将其添加到 OP。
-
能否请您发布将数字转换为Java double 以外的数字的实际代码? (如果它直接进入数据库,那么就会发生这种情况。)
-
if(total!="")不对。那应该是if (!("".equals(total)))或者if (total.length() > 0)。
标签: java android database string listview