Ameena
Ameena

Reputation: 187

To enter in decimal format in textbox

Below code which I am using right now does not give what I require.

textBox1.Text = textBox1.Text + enteredvalue;

I would like to achieve entering decimal value in text box without having to enter ".".

If 3 is entered, the output in text box should be 00.03, and then if 5 is entered the output should be 00.35 and so on.

How do I achieve it?

EDIT: I achieved it by creating a method and calling it everytime i press the input number.

   public void dropcashvalue(string inputdigit)
    {
        if (txtDropCash.Text == "00.00")
        {
            txtDropCash.Text = "";
            this.dropstring = "";
        }
        this.dropstring = this.dropstring + inputdigit;
        txtDropCash.Text = (Convert.ToDouble(this.dropstring) / 100).ToString("F2");
    }

My textbox and inputnumber design looks like this.

enter image description here

Upvotes: 0

Views: 843

Answers (1)

DrKoch
DrKoch

Reputation: 9772

You need to maintain an additional variable for your compund value.

double enteredValue = 0.0;

Whenever a new digit comes in you add it to your value:

enteredValue = enterValue*10 + inputDigit;

Inthe Textbox you show a formatted version of your value:

textBox.Text = (enteredValue/100).ToString("F2");

Upvotes: 2

Related Questions