Hi,
I have two text boxes, One will be used quantity and the other will be used to enter a Unit price.
what I would like to do is :
int or decimal TotalPrice = Unit price * quantity.
what I do in the textbox is:
private void Price_Text_Leave(object sender, EventArgs e)
{
Price_Text.Text = Price_Text.Text.Replace('.', ',');
Double value;
if (Double.TryParse(Price_Text.Text, out value))
Price_Text.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:C2}", value);
else
Price_Text.Text = String.Empty;
}
private void Price_Text_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
}
What I have tried is:
decimal TotalPriceValue = Convert.ToDecimal(UnitPrice.ToString()) * Convert.ToDecimal(Quantity_Text.Text.ToString());
and
int TotalPriceValue = int.parse(UnitPrice.ToString()) * int.parse(Quantity_Text.Text.ToString());
the error I get is :
Input string was not in a correct format.
The error is because of the currency letter(Im my case "R") amd the point "."
I have done this to overcome the error:
string UnitPrice = Price_Text.Text.Replace(',', '.');
UnitPrice = UnitPrice.Replace('R', '0');
UnitPrice = UnitPrice.Replace(' ', '0');
but then I have the issue of inserting the decimal point at the correct place in the number after my calculation is done.
Is there any other calculation I can rather do to help this?
Please excuse my question I am new to c#.