0

In line:

int d = Convert .ToInt32 ( TxtAmount.Text); 

there is an error

Input string was not in a correct format

I want to convert the number inside TxtAmount.Text. if it's a negative number to decimal or integer without - and then convert it again to string because ConvertNumbersToArabicAlphabet the parameter is string.

int d = Convert .ToInt32 ( TxtAmount.Text);
ConvertNumbersToArabicAlphabet a = new ConvertNumbersToArabicAlphabet(d.ToString());
Label2.Text = a.GetNumberAr();
10
  • 3
    What is value of TxtAmount.Text ? Commented Feb 12, 2015 at 9:14
  • try using decimal d = Convert.ToDecimal(TxtAmount.Text); Commented Feb 12, 2015 at 9:15
  • 1
    And why do you want to convert string to number and then convert that number to string? Just use new ConvertNumbersToArabicAlphabet(TxtAmount.Text); Commented Feb 12, 2015 at 9:16
  • What is the value of TxtAmount.Text and what is your CurrentCulture? Debug your code and tell us. Commented Feb 12, 2015 at 9:17
  • Possible doublicate ? : stackoverflow.com/questions/10507759/… Commented Feb 12, 2015 at 9:18

4 Answers 4

1

I'd check whether the input is numeric first and then convert, using Math.Abs to make sure the result is always a positive number:

int result = 0;

// Does text contain numbers only (and maybe a leading "-")?
if (Regex.IsMatch(TxtAmount.Text, @"^-?[0-9]+$"))
{
    // Try to parse an int from it. If successful, convert it to
    // a positive number in any case (= ignore the leading "-")
    if (Int32.TryParse(TxtAmount.Text, out result))
        result = Math.Abs(result);
}

// In all other cases, the result is 0
return result;
Sign up to request clarification or add additional context in comments.

Comments

0
int d = Convert .ToInt ( TxtAmount.Text); 

You can use this if you have a Integer Value within the textbox . If text area contain only a single character then it's not work anymore.

try to put only Integer of same type of data you want to convert in to textbox

like if you want to add a double number then you can use.

Double num = Convert .ToDouble ( TxtAmount.Text); 

if your textbox is empty may sure that you have validation or check for the same:

if(TxtAmount.Text==""||TxtAmount.Text=string.Empty)
{
    TxtAmount.Text=0;
}
else
{
  int d = Convert .ToInt ( TxtAmount.Text); 
}

1 Comment

TxtAmount.Text == "" and TxtAmount.Text == String.Empty are the same thing. It's better to use String.IsNullOrWhiteSpace(TxtAmount.Text).
0

If all you want is to ignore the negative sign, then you can do string manipulation like:

string value = TxtAmount.Text;
if (value.StartsWith("-"))
{
    value = value.Substring(1);
}

Comments

0

This is because Youse textbox is empty. check weather your textbox have values or not

if(!String.IsNullOrWhiteSpace(TxtAmount.Text))
{
   int d = Convert .ToInt32 ( TxtAmount.Text); 
}

1 Comment

As it doesn't work if the text box contains only spaces, it's better to use if (!String.IsNullOrWhiteSpace(TxtAmount.Text)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.