如何解决计算器应用程序中decimalformat的错误

ca1c2owp  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(383)

我正在做一个计算器应用程序。 workingsTV 显示计算的位置。 resultsTV 是显示计算结果的地方。 workings 利用rhino的图书馆做数学。
我想在两边每三位数加一个逗号 workingsTV 以及 resultsTV .
我试着这样用它 resultsTV .

DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
                result = Double.parseDouble(df.format(result));

但后来应用程序关闭了,什么时候显示 result 这是错误消息
原因:java.lang.reflect.invocationtargetexception
原因:java.lang.numberformatexception:对于输入字符串:“1235”
下面是代码的顶部

public class MainActivity extends AppCompatActivity {

    TextView workingsTV;
    TextView resultsTV;

    String workings = "";
    String CLEAR_INT_TEXT;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTextView();
    }

    private void initTextView()
    {
        workingsTV = (TextView)findViewById(R.id.workingsTextView);
        resultsTV = (TextView)findViewById(R.id.resultTextView);
    }

    private void setWorkings(String givenValue)
    {
        workings = workings + givenValue;
        workingsTV.setText(workings);

    }

    public void equalsOnClick(View view)
    {
        Double result = null;
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

        try {
            result = (Double) engine.eval(workings);
            if (result != null)
            {

                DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
                result = Double.parseDouble(df.format(result));

                int intVal = (int) result.doubleValue();
                if (result == intVal)
                {//Check if it's value is equal to its integer part
                    resultsTV.setText(String.valueOf(intVal));
                }
                else
                {
                    resultsTV.setText(String.valueOf(result));
                }
            }
        }
83qze16e

83qze16e1#

我用这个函数把double转换成格式化的字符串

public String formatDouble(double value, int digits) {
    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setGroupingSeparator(',');
    decimalFormatSymbols.setDecimalSeparator('.');
    DecimalFormat decimalFormat = new DecimalFormat("###,##0.00", decimalFormatSymbols);
    decimalFormat.setMinimumFractionDigits(digits);
    decimalFormat.setMaximumFractionDigits(digits);
    return decimalFormat.format(value);
}

在代码中,这里已经有一个结果值 result = (Double) engine.eval(workings); . 你为什么要第二次得到它?此外,使用格式化字符串时,谁可能包含非法的双精度字符(逗号字符)。
把那两条线去掉

DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
            result = Double.parseDouble(df.format(result));

将其设置为textview时设置结果值的格式,例如my函数:

resultsTV.setText(formatDouble(result, 4));

在equalsonclick()方法的末尾,应该设置 result 或者 intValworkings 变量使其为下一个操作做好准备。

workings = String.valueOf(result);
nzkunb0c

nzkunb0c2#

试试这个:

import java.text.NumberFormat;
import java.util.Locale;

Locale locale = new Locale("en", "US");
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(1235.00));

相关问题