I'm trying to write a very simple program to calculate liquid nicotine strengh. Basically it's (strengh / nicStrengh) * amount. And it always comes out as 0.
private void lblCalculate_Click(object sender, EventArgs e)
{
int strengh = Convert.ToInt32(txtBoxDesiredStrengh.Text);
int nicStrengh = Convert.ToInt32(txtBoxNicStrengh.Text);
int amount = Convert.ToInt32(txtBoxAmount.Text);
int result = strengh / nicStrengh * amount;
string resultStr = result.ToString();
label1.Text = resultStr;
}
When you divide integer to integer the result is integer as well; e.g.
5 / 10 == 0 // not 0.5 - integer division
5.0 / 10.0 == 0.5 // floating point division
In your case strengh < amount that's why strengh / amount == 0. If you want result being int (say 3) put it as
int result = strengh * amount / nicStrengh;
if you want double result (i.e. floating point value, say 3.15) let system know that you want floating point arithmetics:
double result = (double)strengh / nicStrengh * amount;
Try this
private void button1_Click(object sender, EventArgs e)
{
int s = int.Parse(textBox1.Text);
int n = int.Parse(textBox2.Text);
int a = int.Parse(textBox3.Text);
int result = (s / n) * a;
label1.Text = result.ToString();
}
or this if result is with comma
private void button1_Click(object sender, EventArgs e)
{
double s = double.Parse(textBox1.Text);
double n = double.Parse(textBox2.Text);
double a = double.Parse(textBox3.Text);
double result = (s / n) * a;
label1.Text = result.ToString();
}
Related
I'm experiencing problem while calculating PAYE. Can someone assist me on how I should be approving this.
Here are the percentage and brackets for PAYE.
Upto 24000, 10%
Between 24001-40667, 15%
Between 40668-57334, 20%
Above 57334, 25%
There's a relief of 2400.
Here's my code:
double grossIncome=Convert.ToDouble(txtgross.Text):
private double getTax(double gross)
{
if(gross <=24000) return(gross*0.1);
if(gross >=24001 | gross <=40667) return(gross*0.15);
if(gross >=40668 | gross <=57334) return(gross*0.2);
return (57335+(gross*0.25);
}
To calculate PAYE:.
private void btnCalculate_Click(object sender,EventArgs e)
{
PAYE=getTax(grossIncome)-2400;
txtPaye.Text=PAYE.ToString();
}
I'm getting less PAYE value.
Try this:
double grossIncome=Convert.ToDouble(txtgross.Text):
private double getTax(double gross)
{
double result = 0;
if (gross <=24000) {
return gross*0.1;
} else {
result += 2400;
}
if (gross >=24001) {
var firstBase = gross - 24000;
result += firstBase * 0.15;
if (gross <= 40667) {
return result;
}
}
var third = gross - 40667;
result += third * 0.2;
if (gross <= 57334) {
return result;
}
var lastbase = gross - 57334;
result += lastbase * 0.25;
return result;
}
Good day everyone! Hope this work. I have to 2 textboxes and I convert it to integer the num1 and num2. I want to display it into textSubtotal. For example 10 * 10 = 100. I want to multiply another 2 numbers the value is 20 * 20 = 400. Add it to 100 so the answer will be 100 + 400 = 500. But the problem is I received an error in this line textSubtotal.Text = Convert.ToString(float.Parse(textSubtotal.Text) + sum) Input string was not in a correct format. Can somebody help me regarding to my problem?
private void buttonOrder_Click(object sender, EventArgs e)
{
float num1, num2, product = 0, sum = 0;
num1 = float.Parse(textPrice.Text);
num2 = float.Parse(textQuantity.Text);
product = num1 * num2; sum = sum + product;
textSubtotal.Text = Convert.ToString(float.Parse(textSubtotal.Text) + sum);
}
You don't need to struggle with Textbox string conversions to do that. Use a private field:
private float subTotal = 0; // this would be a field in your class
private void buttonOrder_Click(object sender, EventArgs e)
{
float num1 = float.Parse(textPrice.Text);
float num2 = float.Parse(textQuantity.Text);
subTotal += num1 * num2;
textSubtotal.Text = subTotal;
}
You should check that the two fields contain actual numeric values (see float.TryParse()). Also, consider using decimal (not float) for this kind of calculations.
Trying to calculate percentage amount but it always return a 0.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
double igst_amt;
double amt = (Convert.ToDouble(row.Cells[dataGridView1.Columns[4].Index].Value)) * (Convert.ToDouble(row.Cells[dataGridView1.Columns[5].Index].Value));
row.Cells[dataGridView1.Columns[6].Index].Value = Convert.ToString(amt);
igst_amt = (igst/100)*(amt);
MessageBox.Show(Convert.ToString(igst_amt));
if (state == "o")
{
row.Cells[dataGridView1.Columns[9].Index].Value =Convert.ToString( (cgst / 100) *( amt));
row.Cells[dataGridView1.Columns[11].Index].Value =Convert.ToString( (sgst / 100) * (amt));
}
else if (state == "i")
{
row.Cells[dataGridView1.Columns[7].Index].Value = igst_amt;
}
double g_total = igst_amt + amt;
row.Cells[dataGridView1.Columns[13].Index].Value = Convert.ToString(g_total);
double t_g_total=0;
t_g_total+= g_total;
}
Let's break this down to the basic code required to reproduce the issue:
int igst = 10;
double amt = 42;
double igst_amt = (igst / 100) * (amt);
Console.WriteLine(igst_amt);
With this I get a result of 0.
However, if I write it like this:
double igst = 10;
double amt = 42;
double igst_amt = (igst / 100) * (amt);
Console.WriteLine(igst_amt);
Then I get 4.2.
I suspect that igst is actually an integer and you're performing integer arithmetic which will result in 0.
You should change igst to double or you can do this:
int igst = 10;
double amt = 42;
double igst_amt = igst * amt / 100;
Console.WriteLine(igst_amt);
A simple change to the calculation and it works.
Also, as a side note, you appear to be doing monetary calculations using double, but you should be using decimal as it is designed for money calculations.
private void textBox1_TextChanged(object sender, EventArgs e)
{
int x = 0;
if (Int32.TryParse(textBox1.Text, out x))
{
var y = 1000000;
var answer = x * y;
displayLabel2.Text = answer.ToString();
}
else
{
displayLabel2.Text = "error";
}
}
All of this code works. But I don't know how to use it if a decimal is inputed. Currently it reads numerical values fine and calculates them fine. But I need it to allow decimal points to be inputted.
Ex. if someone inputed 4.7, then I need 4.7 to be multiplied by 1000000.
You need to use a number type that has precision. You can use either floating types (double or float) or the decimal type.
private void textBox1_TextChanged(object sender, EventArgs e)
{
decimal x = 0;
if (decimal.TryParse(textBox1.Text, out x))
{
var y = 1000000.0M;
var answer = x * y;
displayLabel2.Text = answer.ToString();
}
else
{
displayLabel2.Text = "error";
}
}
You could use decimal instead of int:
decimal x = 0;
if (decimal.TryParse(textBox1.Text, out x))
private void ParametersChanged(object sender, EventArgs e)
{
if (AreArgumentsValid() && klasaBetonaComboBox.SelectedItem != null)
{
var _dMed = Convert.ToDouble(MomentSavijanjaMed.Text);
var _dh = Convert.ToDouble(VisinaPresjekaH.Text);
var _db = Convert.ToDouble(SirinaPresjekaB.Text);
var _dd1 = Convert.ToDouble(UdaljenostArmD1.Text);
var _dd = _dh - _dd1;
var _dFck = Convert.ToDouble(fck.Text);
var _gamaC = 1.50;
gamaCRezultat.Text = _gamaC.ToString();
var dFcd = _dFck /_gamaC;
FcdRezultat.Text = dFcd.ToString();
var _dMiSd = _dMed * 1000 / (_dd * _dd * _db * dFcd);
rezultat.Text = _dMiSd.ToString("F4");
}
from here i need to use variable _dMiSd and _dd in next struct.
private void deformacijaCelikaComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
double epsilonC2;
double ksiRacunski;
double zetaRacunska;
double kARacunski;
double epsilonC2pretpostavka;
double miSdCrtica;
double alphaVRacunski;
double selectedDeformacija = ((NovaDeformacijaArmature)deformacijaCelikaComboBox.SelectedItem).epsilonCelika;
double zeta;
double ksi;
double xNeutralnaOs;
double zKrakSila;
if (selectedDeformacija == 20.0)
{
for (epsilonC2pretpostavka = 0.01; epsilonC2pretpostavka <=3.5 ; epsilonC2pretpostavka += 0.01)
{
ksiRacunski = epsilonC2pretpostavka / (selectedDeformacija + epsilonC2pretpostavka);
if (epsilonC2pretpostavka <= 2)
{
kARacunski = 8 - epsilonC2pretpostavka / (4 * (6 - epsilonC2pretpostavka));
alphaVRacunski = (epsilonC2pretpostavka * (6 - epsilonC2pretpostavka)) / 12;
}
else if (epsilonC2pretpostavka <= 3.5)
{
kARacunski = (epsilonC2pretpostavka * (3 - epsilonC2pretpostavka - 4) + 2) / (2 * epsilonC2pretpostavka * (3 * epsilonC2pretpostavka - 2));
alphaVRacunski = (3 * epsilonC2pretpostavka - 2) / (3 * epsilonC2pretpostavka);
}
zetaRacunska = 1 - (kARacunski * ksiRacunski);
miSdCrtica = 0.85 * alphaVRacunski * ksiRacunski * zetaRacunska;
if (Math.Abs(miSdCrtica-_dMiSd)<0.0001)
{
epsilonC2pretpostavka = epsilonC2;
zetaRacunska = zeta;
ksiRacunski = ksi;
xNeutralnaOs = ksi * _dd;
zKrakSila = zeta * _dd;
}
here i got for loop as you see. Inside of it i have if case which would give me values for variables kAracunski and alphaVracunski.
When i get them from calculation i want to use them for calculation miSdCrtica.
After that i want to check does their difference satisfy given condition.
I get an error:
"Variables does not exist in current context (for _dMiSd and _dd).
And error
"Use of unassigned variable alphaVRacunski and zetaRacunski.
How to link them up together?
Simple example
private void something(object sender, EventArgs e)
{
double a=3;
double b=6;
double c;
double d;
if (a>5)
{
c = a + b;
}
d = c * 5;
variable c is unassigned and it can't be used...how can i use it?
I understand it is pretty dummy question and solution is probabily pretty simple but I am really new in programming and need some time to tie things together. My appologies
Try declaring them outside of the area your modifying their value in and declare them as integers or doubles.
internal int _dMisd;
internal int _dd;
private void ParametersChanged(object sender, EventArgs e)
{
if (AreArgumentsValid() && klasaBetonaComboBox.SelectedItem != null)
{
_dMed = Convert.ToDouble(MomentSavijanjaMed.Text);
_dh = Convert.ToDouble(VisinaPresjekaH.Text);
_db = Convert.ToDouble(SirinaPresjekaB.Text);
_dd1 = Convert.ToDouble(UdaljenostArmD1.Text);
_dd = _dh - _dd1;
_dFck = Convert.ToDouble(fck.Text);
_gamaC = 1.50;
gamaCRezultat.Text = _gamaC.ToString();
dFcd = _dFck /_gamaC;
FcdRezultat.Text = dFcd.ToString();
_dMiSd = _dMed * 1000 / (_dd * _dd * _db * dFcd);
rezultat.Text = _dMiSd.ToString("F4");
}
}
Why are you declaring numbers as 'var' anyway??
EDIT: Declare c and d outside of the function
double c;
double d;
private void something(object sender, EventArgs e)
{
double a=3;
double b=6;
if (a>5)
{
c = a + b;
}
d = c * 5;
}