Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that we understand math operations, let's see if we can calculate a total price for the order.
Cat Food Store Features
Display welcome messageAsk for quantity- Calculate total
- Discount for large orders
Now that we understand math operations, let's see if we can calculate a total price for the order. At the base rate, each can is $2, so we need to multiply the number of cans by 2. We'll worry about applying discounts for larger orders later.
- We'll update the call to
Console.WriteLine
to print the number of cans entered, and then the same number multiplied by 2.
Console.WriteLine($"For {entry} cans, your total is: ${entry * 2}");
- But this generates an error:
Operator '*' cannot be applied to operands of type 'string' and 'int'
- The value returned from
Console.ReadLine
in theAsk
method is astring
. - So the value returned from
Ask
is also astring
. - And you can't multiply a string by a number; it just doesn't make sense.
- We need to convert the string to an actual number before multiplying it.
- We can't use a conversion, though:
int dozen = (int)"12";
- Those only work on numeric types. We'll get an error if we try to use one on a string: "Cannot convert type 'string' to 'int'"
- Instead, all of the built-in types have a static
Parse
method that we can call to convert string values.- I can write the type name I want to convert to,
int
, a dot, the name of theParse
method, parentheses, and the string value I want to convert:int dozen = int.Parse("12");
- That will get me a value of the
int
type. - Remember,
int
is just a shorthand for theSystem.Int32
type; that's why it showsSystem.Int32
here.
- I can write the type name I want to convert to,
int dozen = int.Parse("12");
Console.WriteLine(dozen.GetType()); // => System.Int32
Console.WriteLine(dozen); // => 12
- Be sure that you use a string that will actually convert to the type you want.
- For example, what if I changed this string to hold a floating-point number?
int dozen = int.Parse("12.42398");
- I'll get an error, because it's expecting an integer: "System.FormatException: Input string was not in a correct format."
- And of course, using a string that doesn't even contain a number will fail as well:
int dozen = int.Parse("ADSFAFDS");
- For example, what if I changed this string to hold a floating-point number?
- As I mentioned, every built-in type has a
Parse
method.- You just have to be sure to provide a string that will convert to a value of that type.
double pi = double.Parse("3.1415");
Console.WriteLine(pi.GetType());
Console.WriteLine(pi);
bool flag = bool.Parse("true");
Console.WriteLine(flag.GetType());
Console.WriteLine(flag);
char letter = char.Parse("A");
Console.WriteLine(letter.GetType());
Console.WriteLine(letter);
int number = int.Parse(entry);
Console.WriteLine($"For {number} cans, your total is: ${number * 2}");
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up