货币符号停止计算

时间:2013-06-07 21:20:39

标签: android

我正在尝试进行计算,但TextViews tIncomeNum和1tExpenseNum1中有一个£。有没有办法删除这个£符号?

public class Balance extends Activity 
{
    public static final String PREFS_INCOME = "IncomePreferenceFile";
    public static final String PREFS_EXPENSE = "ExpensePreferenceFile";

    double balance, income, expense;
    Button cal, options;
    TextView tBalance, tBalanceNum, tIncome, tIncomeNum, tExpense, tExpenseNum;
    String sIncome, sExpense;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeVariables();

        SharedPreferences sharedIncome = getSharedPreferences(PREFS_INCOME, 0);
        tIncomeNum.setText(sharedIncome.getString("TotalInNum", "£0"));

        SharedPreferences sharedExpense = getSharedPreferences(PREFS_EXPENSE, 0);
        tExpenseNum.setText(sharedExpense.getString("TotalExNum", "£0"));

        cal.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) 
            {   
                convertToString();
                convertToDouble();
                balance = income - expense;
                DecimalFormat currency = new DecimalFormat("0.00");
                tBalanceNum.setText("£" + currency.format(balance));
                if(balance > 0)
                {
                    tBalanceNum.setTextColor(Color.GREEN);
                }
                else if(balance < 0)
                {
                    tBalanceNum.setTextColor(Color.RED);
                }
                else if(balance == 0)
                {
                    tBalanceNum.setTextColor(Color.BLACK);
                }
            }
        });

        options.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Intent openMenu = new Intent("com.studentbudget.COPYOFMENU");
                startActivity(openMenu);
                finish();
            }
        });
    }

    private void initializeVariables() {
        // TODO Auto-generated method stub
        cal = (Button) findViewById(R.id.bCal);
        options = (Button) findViewById(R.id.bOptions);
        tBalance = (TextView) findViewById(R.id.tvBalance);
        tBalanceNum = (TextView) findViewById(R.id.tvBalanceNum);
        tIncome = (TextView) findViewById(R.id.tvIncome);
        tIncomeNum = (TextView)findViewById(R.id.tvIncomeNum);
        tExpense = (TextView) findViewById(R.id.tvExpense);
        tExpenseNum = (TextView) findViewById(R.id.tvExpenseNum);

    }

    private void convertToString() {
        // TODO Auto-generated method stub
        sIncome = tIncomeNum.getText().toString();
        sExpense = tExpenseNum.getText().toString();
    }

    private void convertToDouble() {
        // TODO Auto-generated method stub
        income = Double.parseDouble(sIncome);
        expense = Double.parseDouble(sExpense);
    }
}

我的计算如下。

inTotal = inLoan + inWage + inGrant + inOther;
DecimalFormat currency = new DecimalFormat(".00");
TotalInNum.setText("£" + currency.format(inTotal));

2 个答案:

答案 0 :(得分:0)

可能不是解决此问题的最佳方式,但如果您只想从字符串中删除“£”,则可以使用此

String stringVar = "£10.00";
stringVar = stringVar.replace("£", "");     //Replaces all '£' occurrences with ''

答案 1 :(得分:0)

如果您的符号位于第一个位置,则可以使用substring(),如下所示

说你的textview文本存储在字符串variable s中,所以现在删除第一个字符使用

s.substring(1);

现在,如果您想将其转换为double并将其用于计算,则可以使用

double value = Double.parseDouble(s.substring(1));