Working with Floating-Point Types3:45 with Carling Kirk
We'll learn about floating-point types.
We've only got one field left to parse in our CSV file. 0:00 Let's take a look. 0:03 Possession percent. 0:06 We can see that it has some values here with decimal points. 0:09 We will need to use a floating point type with this field. 0:13 Let's talk a little bit about the floating point types. 0:16 I'll bring up the C# interactive window again. 0:20 C# interactive, there are three floating point types in C#. 0:24 Float, double, and decimal. 0:29 Double is the default in C#, and 0:33 is usually the one you'll choose when you need a floating point type. 0:35 var myNumber = 12.50. 0:39 If we hover over the myNumber 0:45 variable here, you can see the Visual Studio is inferring that it's a double. 0:50 The float type is a little less common. 0:56 Floats have a smaller size and lower precision than other numbers. 0:59 Only use it if you really care about memory and 1:02 don't really care about rounding errors or precision. 1:05 The third type, the decimal, is a super precise floating point type. 1:09 It's best used to represent currency and 1:14 other types of financial transactions because it's really precise. 1:16 But it also takes up more memory than the other floating point types. 1:20 If we need to assign a number literal to a float variable or 1:24 a decimal variable, we need to use a suffix. 1:27 Let's try it without it. 1:30 Float, my float equals 12. 1:31 Yeah I didn't like that, 1:38 says we need to use an F suffix to create a literal of this type. 1:42 Let's try it with the suffix. 1:47 float myFloat = 12.50F. 1:48 There we go. 1:56 So the same thing goes with the decimal. 1:58 decimal myDecimal = 12.50;. 2:00 Nope. 2:06 It wants an M as the suffix. 2:07 decimal .myDecimal 2:13 = 1250M;. 2:20 And that works. 2:25 So which do you think would be the best for a position percent property? 2:27 I don't think it will need to be super precise. 2:31 A double is our fallback option and it should work just fine. 2:34 public double 2:37 PossessionPercent. 2:41 Now we can parse the values with another try parse method on double. 2:47 Double possessionPercent;. 2:58 And if (double.TryParse(values, 3:01 out possessionPercent)) and we're up to 7 now, 3:08 then gameResult.PossessionPercent 3:15 = possessionPercent;. 3:20 Now let's debug again and see our progress. 3:24 F5. 3:31 Looking good. 3:42
You need to sign up for Treehouse in order to download course files.Sign up