0

I am making a calculator app.

workingsTV is the place where calculating is shown.

resultsTV is the place showing the result of calculating.

workings is doing math by using rhino's library.

I want to add a comma at every three digits on both workingsTV and resultsTV.

I tried to use it like this for resultsTV.

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

But then the app was closed when to show result

This is the error message
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NumberFormatException: For input string: "1,235"

Here is the top part of the code

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));
                }
            }
        }
cloud
  • 77
  • 6

2 Answers2

1

Try this:

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));
Dman Cannon
  • 116
  • 7
1

I'm using that function to convert double to formatted string

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);
}

In your code, you already have an result value here result = (Double) engine.eval(workings);. Why do you want get it second time? In addition, using formatted string, who may contains illegal character for double (comma char).

Just remove that two lines

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

And format result value when you'll set it to TextView, example with my function:

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

At the end of equalsOnClick() method, you should set result or intVal to the workings variable to make it ready for next operations.

workings = String.valueOf(result);
grabarz121
  • 616
  • 5
  • 13
  • Thank you for your example. It works when I put between if (result == intVal) { resultsTV.setText(formatDouble(result, 0)); } – cloud Jan 05 '21 at 12:27
  • But then how can I make it if the number is 1234.1+1 for `else`?. Also, how can I make it possible for `workingsTV`? Sorry that I am a beginner so it's difficult to code well. – cloud Jan 05 '21 at 12:35
  • @WoojuSpace I think you should at the end `equalsOnClick()` method set result as workings. Look my edit. – grabarz121 Jan 05 '21 at 14:32
  • I am reading what you write again and again. But it's so hard to understand. Formatted string cannot be used for Double? So I have to set it up again? – cloud Jan 07 '21 at 06:28
  • If I use `resultsTV.setText(formatDouble(result, 2));` It shows different result for Int and Double. `1000+1=1,001.00` `1000+1.1=1001.1` (At Double, it is not formattted) – cloud Jan 07 '21 at 06:29
  • I cannot understand what you said at the last. Sorry... Do you mean set for Double formatted? Make another method? – cloud Jan 07 '21 at 06:30
  • @WoojuSpace I mean already formatted value. `1,001.00` is not parsable, so `Double.parseDouble("1,001.00")` will return `NumberFormatException`. I don't know, what you pass to `setWorkings()` method, but remember, you cannot do `1,001.00 + 1.1`, because this will cause error. – grabarz121 Jan 07 '21 at 09:17