【问题标题】:How should I stop strings that are numbers from rounding when displayed?我应该如何在显示时阻止数字字符串四舍五入?
【发布时间】: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


【解决方案1】:

这很有趣,因为显然不是double 而不是BigDecimal。 你提到了数据库。也许您从数据库中重新加载字段,记录字段是DOUBLE 而不是DECIMAL,或者DECIMAL 错误precision

【讨论】:

  • 数据库将字符串保存为字符串。另一个进行计算的人使用 blob。我觉得奇怪的是“0.6758431”无法正确显示,但在我添加*后它显示为“0.6758431*”
【解决方案2】:

编辑:

嗯。那么问题似乎不在formatter 对象中。除非调用了setParseBigDecimal(true),否则它将返回Double,但是如果您尝试将Double 转换为BigDecimal,则会出现转换错误。我认为您不需要格式化程序,应该调用以下内容,但我看不出导致截断的位置:

new BigDecimal(total)

怀疑您正在从Stringdoublefloat 的某个地方截断BigDecimal 的精度。例如,你确定你没有这样做吗:

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

确保您使用的是String 构造函数BigDecimal

working = new BigDecimal(valueStr);

这会对字符串进行逐个字符的处理,以创建BigDecimal,而不会损失精度。

如果您提供更多详细信息(就像许多 cmets 要求的那样)并显示实际创建 BigDecimal 的代码,我将编辑我的答案以提供更多信息。


代码:

if(total!="")

不对。您很少希望将字符串与==!= 进行比较。我怀疑应该是:

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

或许

if (total.length() > 0)

【讨论】:

  • 我不显示工作,我显示从不使用 double 或 float 构造自身的方程式。
  • 请编辑您的问题以显示@jersam515 的代码。
  • 好的,我做到了。感谢您调查我的问题。
【解决方案3】:

我最终做的只是在字符串的开头添加一个空格,这很有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多