Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here i give 3 textboxes, quantity, amount and total price in 1 unit comboBox i want do when i enter in total amount textbox any amount like Rs. 51 then automatically calculate quantity in kg or grams,
i dont understand how to i do to calculate quantity acording to amount.

What I have tried:

C#
private void Totaltxt_KeyDown(object sender, KeyEventArgs e)
         {
             if (txttax.SelectedIndex > 0 && Totaltxt.Text != "" )
             {
                 double Quantity1 = Convert.ToDouble(txtqty.Text);
                 double Price1 = Convert.ToDouble(qtytxtprice.Text);
                 double Price2 = 0;
                 double Quantity2 = 1;
                 //float q;
                 try
                 {
                     Price2 = Convert.ToSingle(Totaltxt.Text);
                     Quantity2 = Convert.ToDouble((Quantity1 / Price1) * Price2);
                     txtqty.Text = Quantity2.ToString("F2");
                 }
                 catch { }
             }
         }
Posted
Updated 13-Dec-17 21:20pm
v3
Comments
Richard Deeming 13-Dec-17 10:24am    
Oh, come on! This isn't a code problem; it's a basic arithmetic problem.

Work out how you'd do it on paper, or using a calculator. Then do that in code.

1 solution

double Quantity1 = 1.00;
double Price1 = 60.0;

double Price2 = 51.0;

double Quantity2 = (Quantity1 / Price1) * Price2;
 
Share this answer
 
Comments
CPallini 13-Dec-17 3:18am    
5.
________________ 13-Dec-17 4:09am    
Just going according the rules! ;-)
Deekshaa Singh Chauhan 13-Dec-17 3:47am    
i mean 60rs per kg ,then 51rs me koi buy karega to kya quantity hogi use kese calculate kare
________________ 13-Dec-17 4:05am    
Only so it is correct:
60 rs for 1 kg.
1 kg per 60 rs.

This give proportion, and unknown quantity could be calculated, if one member (51rs) is known.
The result of your example is 850 gram.
________________ 13-Dec-17 4:08am    
1/60== X/51.

X = 1/60 * 51

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900