Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TryParse failing with currency string type for parameter

Tags:

c#

winforms

My form has textbox that holds a number for quantity. then in the textchanged event that number is placed in a label's text property with currency value (ex: $4.00). In my button click event I am trying to add all the values from the labels. When using the following code tryparse fails everytime

int num1;
string text = lbl85x11bwsub.Text;
if (int.TryParse(text, out num1)) 
{
     MessageBox.Show(num1.ToString()); //testing
}
else
{
      MessageBox.Show("it failed");
}

but if I try the same thing using the textbox's text property it works.

int num2;
if (int.TryParse(txt85x11bw.Text, out num2)) 
{
     MessageBox.Show(num2.ToString());
}
else
{
      MessageBox.Show("it failed");
 }
like image 829
KFP Avatar asked Mar 13 '13 19:03

KFP


People also ask

What is the return value of TryParse() method?

It returns a value that indicates whether the conversion succeeded or failed. public static bool TryParse (string value, out bool result); value: It is a string containing the value to convert. result: When this method returns, if the conversion succeeded, contains true if value is equal to TrueString or false if value is equal to FalseString.

When should I use try_parse in Python?

Use TRY_PARSE only for converting from string to date/time and number types. For general type conversions, continue to use CAST or CONVERT. Keep in mind that there is a certain performance overhead in parsing the string value.

What is the TryParse method of Int32?

All numeric primitive data types (int, decimal, Double, float, long , bool etc) has a static TryParse method that is accessible using dot (.) operator as follows. The standard format of Int32.TryParse method is as follows: string str (Input parameter) : A string input value to convert.

How many times did you wish TryParse didn’t use out method parameter?

How many times did you wish that TryParse didn’t use out method parameter? Yes, the same answer again. But how many times did you think how to improve that scenario? Probably never.


Video Answer


2 Answers

@Frederik Gheysels give you almost complete answer on your question except one tiny thing. In this case you should use NumberStyles.Currency because your number can be not only not integer, but also contain thousand and decimal separators. While NumberStyles.AllowCurrencySymbol will only care about currency sign. On the other hand NumberStyles.Currency is a composite number style. Which can allow almost all the other separators.

So, this expression, probably, will works:

Decimal.TryParse(text, 
    NumberStyles.Currency, 
    CultureInfo.CurrentCulture, 
    out result);
like image 140
apros Avatar answered Oct 11 '22 05:10

apros


Try

Decimal.TryParse(text, 
    NumberStyles.Currency, 
    CultureInfo.CurrentCulture, 
    out result);

instead. The number you're trying to parse, is:

  • not an integer
  • contains a currency symbol
like image 31
Frederik Gheysels Avatar answered Oct 11 '22 04:10

Frederik Gheysels