0

I'm trying to do a calculation, however the TextViews tIncomeNum and 1tExpenseNum1 have a £ in it. Is there any way to remove this £ sign?

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);
    }
}

My calculations is below.

inTotal = inLoan + inWage + inGrant + inOther;
DecimalFormat currency = new DecimalFormat(".00");
TotalInNum.setText("£" + currency.format(inTotal));
Tim
  • 41,901
  • 18
  • 127
  • 145
Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60

2 Answers2

0

Probably not the best way to go about this but if you just want to remove the '£' from a string you can use this

String stringVar = "£10.00";
stringVar = stringVar.replace("£", "");     //Replaces all '£' occurrences with ''
Josh Maggard
  • 419
  • 3
  • 9
0

if your symbol is at first position then you can use substring() as below

say your textview's text is stored in string variable s so now to remove first character use

s.substring(1);

now if you want to convert it to double and use it for calculations you can use

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

karan
  • 8,637
  • 3
  • 41
  • 78
  • Where would I use substring()? I've tried putting it into both the convertToString() and covertToDouble() methods. Maybe I'm placing it in the wrong place? It's crashed in both positions – Mark O'Sullivan Jun 07 '13 at 22:31
  • you will get string with symbol removed and if you want to use it as integer you can use `Integer.parseInt(s.substring(1))` – karan Jun 08 '13 at 05:00