Friday, April 20, 2007

Getting user input

In c#, all input from user will be taken as string. we need to convert user input to decimal if we want to use it as a number. C# provide a nice class called Convert to deal with this.

example code:

string temp;
decimal amount;

Console.Write("account 1 deposit: "); // print question
temp=Console.ReadLine(); // get user input as string

// convert to decimal
amount = Convert.ToDecimal(temp);

// print user input with string currency specifier
Console.WriteLine("total deposit:{0:C}", amount);


output:
account 1 deposit: [500] => my input
total deposit:$500.00

No comments: