I have little problem, and I need help..
So here is my problem I have created win form in c# and used numericupdown element to insert my numbers, but I cant calculate percent. Here is the code below:
private void button8_Click(object sender, EventArgs e)
{
int x, y, sum;
x = Convert.ToInt16(numericUpDown7.Value);
y = Convert.ToInt16(numericUpDown8.Value);
sum = x * 3.4528 + 21%;
textBox5.Text = Convert.ToString(sum);
}
What I need to do is to insert x and press button to calculate this formula
example: x * 3.4528 + 21 % = ???
Maby someone has options to help me.
Thanks for all of you, who will help me!
Try this
sum = (x * 3.4528) * 1.21;
private void button1_Click(object sender, EventArgs e)
{
double eng, urdu, math, cs, tot, per;
eng = Convert.ToDouble(txtenglish.Text);
urdu = Convert.ToDouble(txturdu.Text);
math = Convert.ToDouble(txtmath.Text);
cs = Convert.ToDouble(txtcs.Text);
tot = eng + urdu + math + cs;
lbltotal.Text = Convert.ToString(tot);
per = (tot / 400) * 100;
lblpercent.Text = Convert.ToString(per);
}
First off you need to use decimal, float, or double instead of int (you can find many references online about each to help you determine which would be best for you). Otherwise it will just truncate the answer and drop anything after the decimal point. Second you need to use the formula that everyone else has mentioned sum = x * 3.4528 * 1.21.
Related
I am writing a program that first lets a user enter a number between 1 and 10, and then checks what numbers between 1 and 1000 are dividable by that number.
both variables used are int variables, so for example if a user enters 4, it should read: 1 8 12 16 etc.
thusfar i have the following code:
private void button1_Click(object sender, EventArgs e)
{
int Getal1 = 1;
int Getal2;
Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2
for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
{
textBox2.Text = textBox2.Text + "\r\n" + Getal1 / Getal2 + "\r\n";
}
}
the textbook mentions using the following code : Getal1 % Getal2.. i tried adding it after the calculation above, but this didnt work.. math is not my strong point at all..
does someone have an explanation on this?
maybe if Getal1 % Getal2 = 0??.. im really guessing here..
thanks for any help in advance,
Stefan
I tried adding code from the textbook, expecting the program to work correctly, right now it does give a result, but not a correct result.. for example if i enter 2 it starts the sequence with the numbers 123..
for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
{
if (Getal1 % Getal2 == 0)
{
textBox2.Text += Getal1.ToString() + Environment.NewLine;
}
}
You can omit the division checking whatsoever by starting with the number introduced by the user and then adding this number in the loop. This way you will never get a number that is not dividable.
For example:
if user inputs 4, you start with 4
than you add 4 to it and get 8
add 4 again, get 12 and so on
private void button1_Click(object sender, EventArgs e)
{
int Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2
for (int Getal1 = Getal2; Getal1 <= 1000; Getal1 += Getal2)//// for loop.
{
textBox2.Text += Getal1 + "\r\n";
}
}
I generate barcode but it is not active with scanner. I am using C# + SQL server 2102 and using font "#IDAHC39M Code 39 Barcode"
I using this code.
private void btngeneratebarcode_Click(object sender, EventArgs e)
{
string id;
Random barcode = new Random();
int i = barcode.Next();
id = i.ToString();
int k = int.Parse(id);
int x = k + 1;
string y = "0" + x + "0";
lbbarcode.Text = y.ToString();
lbbarcode.Visible = true;
textBox1.Text = x.ToString();
}
I display the barcode to a label and then print the label bar for product code, but while I scan with barcode scanner it is no active, no any result.
It is my first barcode generate. I don''t know that this is the right method.
Thnaks in advance.
Barcodes have specific rules to create them. You are using Code 39 for what I see, so you must add the start/stop char at the first and last position.
Usually the start/stop char is represented by the start (*), so if your font accomplishes that you can modify your code with this:
string y = "*0" + x + "0*";
Else you must change the star to the char used as start/stop by your font.
I want the program to get all of the elem1-elem7 info, add it together, and put it into the totalElem variable. That part works fine.
The part I'm stuck on, is that I want to take that number (lets say 30 for example), and put it on the end of a decimal to use it as a multiplier. Therefore 30 would become 1.30.
The error I'm getting is:
Cannot implicitly convert type 'string' to 'decimal'.
Please note, that is not where the variable definitions really are in the code. I just put them there so I didn't have to post my whole program.
private void calculateButton_Click(object sender, EventArgs e)
{
int startingSheetDPS;
int chd;
int skill;
int elem7;
int elem6;
int elem5;
int elem4;
int elem3;
int elem2;
int elem1;
int totalElem;
decimal elemMultiplier;
decimal baseMultiplier;
elem1 = Convert.ToInt32(ele1.Text);
elem2 = Convert.ToInt32(ele2.Text);
elem3 = Convert.ToInt32(ele3.Text);
elem4 = Convert.ToInt32(ele4.Text);
elem5 = Convert.ToInt32(ele5.Text);
elem6 = Convert.ToInt32(ele6.Text);
elem7 = Convert.ToInt32(ele7.Text);
chd = Convert.ToInt32(chd1.Text);
skill = Convert.ToInt32(skill1.Text);
totalElem = elem1 + elem2 + elem3 + elem4 + elem5 + elem6 + elem7;
elemMultiplier = 1 + "." + totalElem;
}
In short, I want to be able to turn elemMultiplier into a decimal variable, containing 1.totalElem.
Ok, a really dirty and fast way, replace your
elemMultiplier = 1 + "." + totalElem;
with
elemMultiplier = decimal.Parse("1." + totalElem);
Be ware, this is locale-dependant.
Use this:
String elemMul = "1." + totalElem.ToString();
elemMultiplier = Convert.ToDecimal(elemMul);
Your code shows problem because "." is a string which cannot be converted to decimal implicitly.
Don't concatenate strings. Just do the math:
elemMultiplier =
Convert.ToDecimal(1 + (totalElem / Math.Pow(10, totalElem.ToString().Length)));
(Edited after Gusman noticed a problem.)
The Task:
Create a hourly pay calculator which is simple to use but effective.
private double amount4Hours = 4;
private double amount8Hours = 8;
private double amount10Hours = 10;
private void btnSubtotal_Click(object sender, EventArgs e)
{
double answer;
// 45 min break removal
double break45 = 0.75;
double outputValue = 0;
bool isNumber = true;
//true false statement for error checking
isNumber = double.TryParse(text4Hours.Text, out outputValue);
isNumber = double.TryParse(text8Hours.Text, out outputValue);
isNumber = double.TryParse(text10Hours.Text, out outputValue);
if (!isNumber)
{
//error checking for blank text boxes
MessageBox.Show("Enter a number from 0-9");
}
else
{
//calculates total amount of hours after breaks have been removed
amount4Hours = amount4Hours * double.Parse(text4Hours.Text);
amount8Hours = amount8Hours * double.Parse(text8Hours.Text) -
break45 * double.Parse(text8Hours.Text);
amount10Hours = amount10Hours * double.Parse(text10Hours.Text) -
break45 * double.Parse(text10Hours.Text);
// Adds all together to output final amount of hours
answer = amount4Hours + amount8Hours + amount10Hours;
labSubtotal.Text = answer.ToString();
}
}
private void btnPay_Click(object sender, EventArgs e)
{
// Hourly pay stored here
double hourpay = 6.19;
hourpay = hourpay * double.Parse(labSubtotal.Text);
labPay.Text = hourpay.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
// Resets all text boxes back to blank
text4Hours.Text = string.Empty;
text8Hours.Text = string.Empty;
text10Hours.Text = string.Empty;
labSubtotal.Text = string.Empty;
labPay.Text = string.Empty;
}
}
}
The Problem...
When I type in three different numbers in each text box, I get the outcome just perfect.
If I hit the clear button, it does what I ask and removes everything from the output
If I enter three numbers again (same ones or different ones) after it has been cleared, I will get different output.
I think it has something to do with the clear code because it's not resetting the values to zero like it does at the start of the program. I have tried setting the clear code to input zeros, but that doesn't help; just gives the same problem.
This is a good case to show how to use the debugger. Put a breakpoint on the line:
amount4Hours = amount4Hours * double.Parse(text4Hours.Text);
Then when you calculate the answer, watch how the amount4Hours variable changes.
This type of bug shows why people avoid the use of global variables.
private double amount4Hours = 4;
private double amount8Hours = 8;
private double amount10Hours = 10;
This code should go into your btnSubtotal_Click.
amount4Hours = 4;
amount8Hours = 8;
amount10Hours = 10;
Put this code in clear button. You also need to reset your global variables.
As others have said, on a general point you need to use the debugger for figuring out why something hasn't worked as you expected. In this case you are creating 3 global variables at the beginning (amount4hours etc.) and then manipulating them later on when clicking btnSubmit. At no point when btnClear do you reset your global values. Try adding in your btnClear_Click method:
amount4hours = 4;
amount8hours = 8;
amount10hours = 10;
This will reset your global variables.
If the global variables value should not be updated,why not directly get the value of answer by having their sum value directly
/*amount4Hours = amount4Hours * double.Parse(text4Hours.Text);
*
* amount8Hours = amount8Hours * double.Parse(text8Hours.Text) -
* break45 * double.Parse(text8Hours.Text);
*
* amount10Hours = amount10Hours * double.Parse(text10Hours.Text) -
* break45 * double.Parse(text10Hours.Text);
*
* answer = amount4Hours + amount8Hours + amount10Hours;*/
answer = amount4Hours * double.Parse(text4Hours.Text) +
amount8Hours * double.Parse(text8Hours.Text) -
break45 * double.Parse(text8Hours.Text) +
amount10Hours * double.Parse(text10Hours.Text) -
break45 * double.Parse(text10Hours.Text);
By the way, your error checking is fail because you stored all checking result in same variable therefore only the third textbox content is checked based on your logic. I assume you display the MessageBox whenever one of three textbox have invalid input. Then,
//bool isNumber = true;
//true false statement for error checking
//isNumber = double.TryParse(text4Hours.Text, out outputValue);
//isNumber = double.TryParse(text4Hours.Text, out outputValue);;
//isNumber = double.TryParse(text10Hours.Text, out outputValue);
//if (!isNumber)
if (!double.TryParse(text4Hours.Text, out outputValue)||
!double.TryParse(text8Hours.Text, out outputValue)||
!double.TryParse(text10Hours.Text, out outputValue))
{
//error checking for blank text boxes
//the checking only check if they are double type
//but not checking the range from 0 to 9
MessageBox.Show("Enter a number from 0-9");
}
I have the best part of 2 days trying to figure out how to "times 2 dropdownlists" with each other and show it in a label...
like in math when u mutiple 2 numbers..
plzz help me...
all suggestions are welcome
Protected void Button1_Click(object sender, EventArgs e)
{
int amount1 = int.parse(DropDownList1.selectedValue.ToString());
int amount2 = int.parse(DropDownList1.selectedValue.ToString());
Label.Text = amount1 * amount2;
}
i get an error:
Cannot implicitly convert type 'Int' to 'string
Just use:
Label.Text = (amount1 * amount2).ToString();
as you cannot assign integer result to string Label.Text property.
You can also do it this way
int val = amount1 * amount2;
Label.Text = val.ToString();
Label.Text expects String and you were trying to assign integer to it.