Skip to content

Floats

A float is a floating point number.

It’s called that because it stores a fractional number where the decimal point (well, it’s actually a binary point) can move - as opposed to in fixed point notation).

The short answer is that floating point variables store decimal numbers!

You can declare and initialise a variable with a float datatype in C# like this:

float myFloat = 5.7;

You may be wondering: why would we use a new type if the float type can already represent a huge range of values?

The answer is because float is not very precise, and you can tell when working with very big (or very small) numbers.

To solve this, we can use the float data type, which stores a number using 64 bits instead of float’s 32 bits.

Declare and initialise a variable like this:

double myDouble = 3.14159265358979

Unlike integers, all floating point values are signed.

This means that both floats and doubles can represent either positive or negative values.

Floating point calculations don’t always give you what you want.

A common solution to this is ‘loose equality’, which I’ll cover here soon :)