7 Replies - 1534 Views - Last Post: 15 February 2012 - 10:20 AM Rate Topic: -----

#1 rbt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Converting a textbox and removing a character ($)

Posted 14 February 2012 - 07:48 PM

Help Please
I have a dollar amount brought into a textbox from another form that I want to have the retain dollar sign in the text box, but I need to take the number in a math formula after it is entered meaning I need to remove the dollar sign for the formula. This is the only place I use the number. I cant figure out what to do I have tried several things but so far none have worked. This is the area of the code I am having problems with:

public partial class LoanApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoanYearsTextBox.Text = Session["LoanYears"].ToString();
        LoanAmountTextBox.Text = Session["TotalCost"].ToString();
        ZipCodeTextBox.Text = Session["ZipCode"].ToString();
    }
    protected void LoanAmountTextBox.Text(object sender, TextChangedEventArgs e)
    {
        LoanAmountTextBox.Text = LoanAmountTextBox.Text.Replace("$", "");
        LoanAmountTextBox.Select(LoanAmountTextBox.Text.Length, 0);
    } 
    
    protected void CalcButton_Click(object sender, EventArgs e)
    {
        double Total = 0;
       
        int ZipCode;
        Total = double.Parse(LoanAmountTextBox.Text);
        taxes = (Total * interest / term / 12);
        MonthlyRate = (interest / 100 / 12);
        term = (term * 12);
        MonthlyPayment = (Total * (MonthlyRate / (1 - Math.Pow((1 + MonthlyRate), -term))));

    }



Not all the code is in there.
Any help would be greatly appreciated.

This post has been edited by modi123_1: 14 February 2012 - 08:13 PM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Converting a textbox and removing a character ($)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9093
  • View blog
  • Posts: 34,145
  • Joined: 12-June 08

Re: Converting a textbox and removing a character ($)

Posted 14 February 2012 - 08:14 PM

Have you thought about using the currency formatting?

http://msdn.microsof...y/dwhawy9k.aspx
Was This Post Helpful? 0
  • +
  • -

#3 rbt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Re: Converting a textbox and removing a character ($)

Posted 14 February 2012 - 08:42 PM

modi123_1
Actually yes I have, that is how it got it in the first place in the previous form. Maybe I am not seeing my mistake, but I keep getting the error that: Total is not in the correct format: I will provide the whole code and if you would tell me what I am doing wrong, I added in the the Protected void Loan amount... area as the last attempt to fix this, I know it may seem simple to you guys but I am still very much learning and havent been able to figure out what is wrong with this particular area other than it has the dollar sign when brought in from the session.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class LoanApp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoanYearsTextBox.Text = Session["LoanYears"].ToString();
        LoanAmountTextBox.Text = Session["TotalCost"].ToString();
        ZipCodeTextBox.Text = Session["ZipCode"].ToString();
    }
    protected void LoanAmountTextBox.Text(object sender, TextChangedEventArgs e)
    {
        LoanAmountTextBox.Text = LoanAmountTextBox.Text.Replace("$", "");
        LoanAmountTextBox.Select(LoanAmountTextBox.Text.Length, 0);
    } 
    
    protected void CalcButton_Click(object sender, EventArgs e)
    {
        ErrorMsg.InnerHtml = "";
        bool err = false;
        string msg = "Stop<br />";

        Random rnd = new Random();
        int CreditScore1 = rnd.Next(550, 750);
        int CreditScore2 = rnd.Next(550, 750);
        int CreditScore3 = rnd.Next(550, 750);
        CreditScore1TextBox.Text = CreditScore1.ToString();
        CreditScore2TextBox.Text = CreditScore2.ToString();
        CreditScore3TextBox.Text = CreditScore3.ToString();
        double MonthlyRate = 0;
        //double TotalCost = 0;
        double MonthlyPayment = 0;
        double Score1 = 0;
        double Score2 = 0;
        double Score3 = 0;
        double AvgScore = 0;
        double income = 0;
        double interest = 4.0;
        double taxes = 0;
        int term = 0;
        double Total = 0;
       
        int ZipCode;
        Total = double.Parse(LoanAmountTextBox.Text); 
        ZipCode = int.Parse(ZipCodeTextBox.Text);
        term = int.Parse(LoanYearsTextBox.Text);
        Score1 = double.Parse(CreditScore1TextBox.Text);
        Score2 = double.Parse(CreditScore2TextBox.Text);
        Score3 = double.Parse(CreditScore3TextBox.Text);
        income = double.Parse(TotalIncomeTextBox.Text);
        AvgScore = ((Score1 + Score2 + Score3) / 3);

        try
        {
            if (AvgScore < 620)
            {
                throw new System.FormatException();
            }
            else if (income < 50000.00)
            {
                throw new System.FormatException();
            }
        }
        catch (System.FormatException fe)
        {
            err = true;
            msg += "YOU ARE TOO HIGH RISK FOR A LOAN AT THIS TIME";
        }
        if (err)
        {
            ErrorMsg.InnerHtml = msg;
            return;
        }

        if (AvgScore >= 620 && AvgScore <= 690)
        {
            interest = (interest + 2);
        }
        else if (AvgScore >= 651 && AvgScore <= 690)
        {
            interest = (interest + 1);
        }
        else if (AvgScore >= 691 && AvgScore <= 720)
        {
            interest = (interest + 0.5);    
        }
        if (income >= 50000 && income <= 75000)
        {
            interest = (interest + 1);
        }
        else if (income > 75000 && income <= 100000)
        {
            interest = (interest + 0.5);
        }
        InterestRateTextBox.Text = interest.ToString();

        if (ZipCode == 78205 || ZipCode == 78459)
        {
            taxes = 0.0325;
        }
        else
        {
            taxes = 0.028;
        }


        
        taxes = (Total * interest / term / 12);
        MonthlyRate = (interest / 100 / 12);
        term = (term * 12);
        MonthlyPayment = (Total * (MonthlyRate / (1 - Math.Pow((1 + MonthlyRate), -term))));
        TotalOverLifeTextBox.Text = (MonthlyPayment * (term * 12)).ToString("C");
        MonthlyPaymentTextBox.Text = (MonthlyPayment + taxes).ToString("C");
    }

}


This post has been edited by CodingSup3rnatur@l-360: 15 February 2012 - 01:43 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting a textbox and removing a character ($)

Posted 15 February 2012 - 03:15 AM

There is no need for you to remove the currency symbol. Double.Parse (along with all the other number types) has a little used (and seemingly unknown) overload that allows you to specify some information about the number. This is the Double.Parse(String, NumberStyles) method.

You can change your line to
Total = Double.Parse(LoanAmountTextBox.Text, NumberStyles.Currency);


This allows your user to enter "123.45", "$1234.56", "$1,234.56" and the program won't crash with invalid characters :)
Was This Post Helpful? 2
  • +
  • -

#5 rbt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Re: Converting a textbox and removing a character ($)

Posted 15 February 2012 - 06:52 AM

Momerath
I think you are misunderstanding what my problem is, The session brings the number "TotalCost" in to the textbox with the dollar sign and when I bring it back out of the textbox for use in the math formula it still has it on it. This I think is where my code and math is failing because that is where during debugging I cannot get total to equal the amount of the textbox brought over by the session. I dont want to change the value in the textbox just use the value in the formula for other textboxes. In other words it is already currency, I dont want it to be!
But, I appreciate everything you have said so far.

rbt
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9093
  • View blog
  • Posts: 34,145
  • Joined: 12-June 08

Re: Converting a textbox and removing a character ($)

Posted 15 February 2012 - 07:33 AM

Hmmm. Okay - then how about having a function you call that returns your textbox's string (sans the $) and have everything call that instead of <textbox>.text

Use something like the string replace on the dollar sign and replace it with the string.empty..
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting a textbox and removing a character ($)

Posted 15 February 2012 - 08:54 AM

View Postrbt, on 15 February 2012 - 06:52 AM, said:

Momerath
I think you are misunderstanding what my problem is, The session brings the number "TotalCost" in to the textbox with the dollar sign and when I bring it back out of the textbox for use in the math formula it still has it on it.

The method I suggested will give you the number so you can use it in math. The double you get won't 'know' that it was once a currency, the parse option just allows people to enter values how they feel comfortable (with '$', ',', etc.)
Was This Post Helpful? 1
  • +
  • -

#8 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Converting a textbox and removing a character ($)

Posted 15 February 2012 - 10:20 AM

I would have just have used string.Substring to trim the first character off of the numbers string when I take it out of the textbox.

But Momerath's method sounds a lot more flexible and tidy ;)

This post has been edited by negligible: 15 February 2012 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1