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

有帮助吗?

解决方案

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!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top