Help) float and doubles in C#

I was following the Learn C# course on codeacademy and I have a question about float and double in C#

Course Problem`

Next, create a variable to hold the weight. Name the variable weight and store the value 65.22 .


List item

using System;

namespace Form
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create Variables
			string name = "Shadow";
      string breed = "Golden Retriever";
      int age = 5;
      float weight = 65.22;

      // Print variables to the console



    }
  }
}

I tried to actually declare the weight variable as a float initially and came upon an error message:

Program.cs(13,22): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type [/home/ccuser/workspace/csharp-data-types-variables-creating-variables-c-sharp/e7-workspace.csproj]

The build failed. Please fix the build errors and run again.

I thought double was an unnecessary declaration here because the number is small and float is supposed to accept decimal numbers as its value;

Initially I thought putting the ‘F’ or ‘f’ suffix would solve the problem but
Step#3 of the lesson:
“3.Next, create a variable to hold the weight. Name the variable weight and store the value 65.22.”

Still gave me an error msg: “Did you define a double named weight with the value 65.22?”

So is this only because of how this lesson is set up or am I not able to declare “65.22” as a float?
If 65.22 is not in the allowed range, what are some rules I could refer to when creating variables rather than having the range (+/-3.4 * 10^38)?

To declare a float with a value you have to write it as:
float weight = 65.22f;

I think the lesson wants you to use a double specifically.

3 Likes