Format user inputed currency

644 Views Asked by At

Here's my problem, I'm trying to format a {"C:O"} into a console.readline but I'm getting a method name expected error. here's what I have now:

money = double.Parse(Console.ReadLine()(string.Format("{O:C}")));
1

There are 1 best solutions below

0
On

In addition to the syntax errors, you should generally use decimal to represent money, since many operations on double can result in round-off errors.

I'd recommend something like this:

string input = Console.ReadLine();
decimal money = decimal.Parse(input);

Or in one line:

decimal money = decimal.Parse(Console.ReadLine());

But Parse will throw an exception if given invalid input (e.g. "foo"). You might want to use TryParse to be a bit safer:

decimal money;
if (!decimal.TryParse(Console.ReadLine(), out money))
{
    Console.WriteLine("Invalid input");
}