Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

XML
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:
C#
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#.
Posted

1 solution

Why are you managing everything as string? A string is not very useful when it comes to make computations.
What you have to do is to get some decimal from your inputs, and make the computations on these decimals.
Something like:
C#
decimal price;
int quantity;
decimal totalPriceValue;
if (decimal.TryParse(Price_Text.Text, out price) && int.TryParse(Quantity_Text.Text, out quantity)) {
   totalPriceValue = price * quantity;
}
else {
   // Here there was a problem parsing the price and/or the quantity. You have to decide what to do in this case
}


Hope this helps.

PS: there is no point in calling ToString() on a property which already is a string (i.e., the Text property of your TextBoxes).
 
Share this answer
 
v2
Comments
Dr Drastic 17-Jun-14 7:03am    
Hi Phil,

Thanks for your solution. The reason it is managed as string is because of the text in the text box.

in my case it has the currency symbol in the beginning of the text box(for example "$ 10.00").

Thanks for the tips!
Matt T Heffron 17-Jun-14 13:07pm    
The currency symbol can be handled by using the 4 argument form of decimal.TryParse:
if (decimal.TryParse(Price_Text.Text, System.Globalization.NumberStyles.Currency, CultureInfo.CurrentCulture, out price) ... etc.
Dr Drastic 18-Jun-14 5:37am    
Thanks Matt,

Your comment helped allot!!!

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