i am working on a inventory software and was calculating the discount by percentage and amount as well.
- means when i enter % amount it automatically convert to price#
- and when i enter price it automatically converts to the % #
the code i am using to convert price in % is this is:
if(txtDiscount.Text == "" || txtGrandTotal.Text == "")
{
MessageBox.Show("Please Enter amount in Grand Total",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
// double percent;
double grandTotal;
double per;
double txDiscount;
//percent = Convert.ToInt32(txtPercant.Text);
grandTotal = Convert.ToInt32(txtGrandTotal.Text);
txDiscount = Convert.ToInt32(txtDiscount.Text);
per = txDiscount / grandTotal * 100;
//per = percent / 100 * grandTotal;
if (per % 3 != 0)
per = (per - per % 3) + 3;
txtPercant.Text = Convert.ToString(per);
................
................
................
................
................
now let's suppose if i am entering the amount is "25", then the percentage it count is "2.77777777777778". i want to limit this to three decimals after point, like i want to to be "2.778" or whatever means i want to to show only 3 decimals after point, can any one help me out in which way i can do it ?? any suggestions would be appreciated!!
EDIT:
Coversion from Price to % Code is:
private void txtDiscount_Leave(object sender, EventArgs e)
{
if(txtDiscount.Text == "" || txtGrandTotal.Text == "")
{
MessageBox.Show("Please Enter amount in Grand Total",Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
// double percent;
double grandTotal;
double per;
double txDiscount;
//percent = Convert.ToInt32(txtPercant.Text);
grandTotal = Convert.ToInt32(txtGrandTotal.Text);
txDiscount = Convert.ToInt32(txtDiscount.Text);
per = txDiscount / grandTotal * 100;
//per = Math.Round(per, 3);
per = Math.Truncate(per * 1000) / 1000;
txtPercant.Text = Convert.ToString(per);
double lblgrandTotal = 0.0;
double Disconunt = 0.0;
double netTotal = 0.0;
lblgrandTotal = Convert.ToDouble(txtGrandTotal.Text);
Disconunt = Convert.ToDouble(txtDiscount.Text);
netTotal = grandTotal - Disconunt;
//netTotal = Convert.ToInt32(grandTotal) - Convert.ToInt32(Discount);
lblNetTotal.Text = Convert.ToString(netTotal);
}
}
Cobersion from % to Price Code is :
private void textBox1_Leave(object sender, EventArgs e)
{
if (txtPercant.Text == "")
{
MessageBox.Show("Please Enter value in Percenent", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
double percent;
double grandTotal;
double per;
percent = Convert.ToDouble(txtPercant.Text);
grandTotal = Convert.ToDouble(txtGrandTotal.Text);
per = percent / 100 * grandTotal;
txtDiscount.Text = Convert.ToString(per);
double lblgrandTotal = 0.0;
double Disconunt = 0.0;
double netTotal = 0.0;
lblgrandTotal = Convert.ToDouble(txtGrandTotal.Text);
Disconunt = Convert.ToDouble(txtDiscount.Text);
netTotal = grandTotal - Disconunt;
//netTotal = Convert.ToInt32(grandTotal) - Convert.ToInt32(Discount);
lblNetTotal.Text = Convert.ToString(netTotal);
}
}
but the problem i am facing is i am converting % to price and Vise Versa, now in case i am adding 12 in Price it says 12.33%, and when i put 12.33% it converts back it to 11.997Price which is not the same ans it should be... it should be exactly 12 not 11.997... really confused in it..!!
NOTE:
by price i mean that i am calculating % discount and price discount on grand total, means if i add 12% then it calculate the price discount of grand total and if i enter the price amount then it calculates the %discount of grand total