簡體   English   中英

單選按鈕-小數位選項

[英]Radio buttons - decimal place option

我正在編寫一個程序,可以為您執行某些類型的數學運算。 用戶輸入一個數字,然后單擊將其帶到另一個活動的按鈕,並顯示答案以及公式等等。但是,有時答案的小數點后有一個瘋狂的數字。

現在,我已經知道如何更改小數點后的位數,但是我想使用帶有editText的同一頁上的單選按鈕,向用戶提供他們想要的小數點后位數的選項。

我該怎么辦?

這就是我到目前為止所擁有的...

帶單選按鈕的輸入頁面

公眾無效點擊(查看視圖){

    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()){
        case R.id.radioButton1:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.0";
                check.putExtra("PATTERN", value);
            }
        break;
        case R.id.radioButton2:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.00";
                check.putExtra("PATTERN", value);
            }
            break;
        case R.id.radioButton3:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.000";
                check.putExtra("PATTERN", value);
            }
            break;
        case R.id.radioButton4:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.000000000";
                check.putExtra("PATTERN", value);
            }
            break;
    }

}

輸出頁面

Intent intent = getIntent();
String pattern = intent.getStringExtra("PATTERN");
String answerS = intent.getStringExtra("MESSAGE");

double fix = Double.parseDouble(answerS);
DecimalFormat dFormatter = new DecimalFormat(pattern);
String answerSS = "" + dFormatter.format(fix);

TextView answer = findViewById(R.id.answer);
answer.setText(answerSS);

這是布局以及我想要的選項

根據每個RadioButton使用DecimalFormatter

double number = 3.14159265359;
DecimalFormat numberFormat = new DecimalFormat("#.0000");
System.out.println(numberFormat.format(number));

將輸出: 3.1415

您可以通過使用或多或少的0或在定界符后使用#來擴展它,例如:

double number = 3.1500000000;
DecimalFormat numberFormat = new DecimalFormat("#.####");
System.out.println(numberFormat.format(number));

讓您獲得輸出3.15

換這個東西..

switch (view.getId()){
    case R.id.radioButton1:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "0";
            check.putExtra("PATTERN", value);
        }
    break;
    case R.id.radioButton2:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "00";
            check.putExtra("PATTERN", value);
        }
        break;
    case R.id.radioButton3:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "000";
            check.putExtra("PATTERN", value);
        }
        break;
    case R.id.radioButton4:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "000000000";
            check.putExtra("PATTERN", value);
        }
        break;
}





    Intent intent = getIntent();
    String pattern = intent.getStringExtra("PATTERN");
    String answerS = intent.getStringExtra("MESSAGE");

    double fix = Double.parseDouble(answerS);
    DecimalFormat dFormatter = new DecimalFormat("#."+pattern);
    String answerSS = "" + dFormatter.format(fix);

    TextView answer = findViewById(R.id.answer);
    answer.setText(answerSS);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM