電卓アプリを作っています。
workingsTV
計算が表示される場所です。
resultsTV
計算結果を表示する場所です。
workings
rhinoのライブラリを使用して数学を行っています。
両方の3桁ごとにカンマを追加したい
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:入力文字列の場合: "1,235"
これがコードの上部です
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));
}
}
}
回答 2 件
これを試して:
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));
関連記事
- LaTeXでテキストの広すぎるエラーを解決するにはどうすればよいですか?
- 多数のテーブルを追加する際のエラーを解決する方法
- responseJSONの解決方法:{エラー:「invalid_request」、error_description:「必要なパラメーターがありません:コード」}
- PHPでこの未定義のインデックスエラーを解決する方法
- これを解決するにはどうすればよいですか?MSPowerShellの解析エラーだと思いますか?
- エラーレポート付きの2進化10進計算機
- このMemgraphSSL接続エラーを解決するにはどうすればよいですか?
- このエラーを解決するにはどうすればよいですか?「オブジェクトを型として使用しないでください」?
- このCプログラムで型再定義エラーを解決する方法
私はその関数を使用してdoubleをフォーマットされた文字列に変換しています
あなたのコードでは、あなたはすでにここに結果値を持っています
result = (Double) engine.eval(workings);
。なぜあなたはそれを二度手に入れたいのですか?さらに、フォーマットされた文字列を使用します。この文字列には、double(カンマ文字)の不正な文字が含まれている可能性があります。その2行を削除するだけです
そして、TextViewに設定するときに結果値をフォーマットします。たとえば、私の関数を使用します。
equalsOnClick()メソッドの最後に、次のように設定する必要があります
result
またはintVal
にworkings
次の操作の準備をするための変数。