How do I make it so that the user only enters numbers in Console.ReadLine?

14 hours ago 1
ARTICLE AD BOX

Use decimal.TryParse() instead of Convert.ToDecimal(), it'll return false on a bad input

decimal valor; while (true) { Console.Write("Enter a number: "); if (decimal.TryParse(Console.ReadLine(), out valor)) break; Console.WriteLine("Invalid input. Please enter a number."); }

user32581585's user avatar

New contributor

user32581585 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Sign up to request clarification or add additional context in comments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article