0
static void Main()
{        
    Console.Write("Please input a number: ");
    Console.WriteLine("\n The number you selected was {0} \n", method());
}

static int method()
{
    int var = int.Parse(Console.ReadLine());
    return var;
}

The above code throws a format exception. I tried storing the input in a string variable and then parsing, but it had the same problem. I also tried using the Convert class and still had the same problem. I would appreciate if someone could show me where I am wrong.

I am trying to convert 23.4, for example. (It works for Natural numbers but why not 4345.5, for example)

12
  • 1
    What have you entered into the console? Commented Oct 27, 2014 at 16:20
  • 6
    Is 23.4 an integer? Commented Oct 27, 2014 at 16:21
  • 1
    Because int.Parse() will parse an integer number, if you write 4345.5 it'll throw an exception because it's not integer. Use float.Parse() or (better) int.TryParse/float.TryParse Commented Oct 27, 2014 at 16:23
  • 1
    @StefanosVakirtzis see my answer. You still need the float part but you can return only the integer part. 3.6 becomes 3, for example. Commented Oct 27, 2014 at 16:38
  • 1
    @StefanosVakirtzis: you can use the code you've accepted to get the double. Then use (int)number to get an integer where the decimal part is simply truncated. If you want that it gets rounded use Math.Round first. Commented Oct 27, 2014 at 16:38

4 Answers 4

4

23.4 is not an integer, so you cannot use int.Parse (or int.TryParse). Instead you have to parse it to a decimal number like decimal or double. You can use the TryParse methods like Double.TryParse to prevent an exception if it's not a valid number:

string input = Console.ReadLine();
double number;
if(double.TryParse(input.Trim(), out number))
{
    // valid number
    Console.WriteLine("\n The number you selected was {0} \n", number);
}
else
{
     Console.WriteLine("Please enter a real number.");
}

Update from your comments i can see that you want to display an integer.

You can use (int)number to get an integer where the decimal part is simply truncated. If you want that it gets rounded use Math.Round first.

int integer = (int) number; // decimal part is truncated
integer = (int) Math.Round(number, 2, MidpointRounding.AwayFromZero); // rounded to two digits

If you just want to display a string without the decimal part you can also use format strings.

string numberString = number.ToString("N0");
Sign up to request clarification or add additional context in comments.

Comments

1

You should be using double or float for rational numbers:

float num = float.Parse(Console.ReadLine())

Comments

1

You can return the integer part of your input string (3.6 becomes 3, for example) like this:

static int method()
{
    float var = float.Parse(Console.ReadLine());
    return (int)Math.Floor(var);
}

Comments

0

Integer32.Parse is for parsing 32 bit integers. It cannot parse numbers that cannot be represented as a 32 bit integer, such as 23.4, and it will throw an exception when it cannot parse the data. Use a different numeric representation if you wish to represent non-integer numbers.

Comments

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.