Search code examples
androidbigdecimal

How to display EditText in a TextView after changing it to BigDecimal?


I want to get a precise number from an EditText then change in to a BigDecimal and show it in another TextView. Here's a part of my code:

BigDecimal rate_string = new BigDecimal(0);
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = (BigDecimal) rate.getText();
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());

But it doesn't work! What should I do? Thanks


Solution

  • BigDecimal rate_string = new BigDecimal(0);
    EditText rate = (EditText) findViewById(R.id.rate_edittext);
    rate_string = (BigDecimal) rate.getText();
    TextView test = (TextView) findViewById(R.id.textView1);
    test.setText(rate_string.toString());
    

    to

    BigDecimal rate_string; // P.S here is useless the creation
    EditText rate = (EditText) findViewById(R.id.rate_edittext);
    rate_string = new BigDecimal( rate.getText().toString() );
    TextView test = (TextView) findViewById(R.id.textView1);
    test.setText(rate_string.toString());
    

    You cannot convert a String to a BigDeciaml using a cast but BigDecimal have a constructor which takes the value as String.

    P.S Make sure you pass a correct number to the constructor or it will throw a NumberFormatException!