C# Error – Literal of type double cannot be implicitly converted to type ‘float’; use an ‘F’ suffix to create a literal of this type

There are times when you get the below error when building a C# application.

C# Error – Literal of type double cannot be implicitly converted to type ‘float’; use an ‘F’ suffix to create a literal of this type

image

You would get the above error when trying to execute this float floatVal = 10.5; from the below code snippet.

using System;
using System.Collections.Generic;

namespace DeveloperPublishCodeConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            float floatVal = 10.5;
            Console.ReadLine();
        }
    }
}

In the above code snippet , the error was caused because the value 10.5 is by default inferred to be of type double . This does not have implicit conversion to float .

Using the suffix “F” for the value will fix this issue as shown below.

float floatVal = 10.5F;
Console.ReadLine();

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...