What is a data type of the following variable? var number = 1.0;

Experience Level: Senior
Tags: .NETC#

Answer

Is it float, double and decimal?

It's double. Which is 64bit double precision floating point data type.

How to verify

Just run he following program and see the output:

var number = 1.0;
Console.WriteLine(number.GetType());

Things to remember

  • float = 32bit single precision floating point data type (7 digits)
  • double = 64bit double precision floating point data type (15-16 digits)
  • decimal = 128bit floating point data type (28-29 significant digits)

When to use which type?

Can't you risk rounding error? Are you counting money? Use decimal.

Can you have rounding error and do you require higher number? Use double.

Can you have rounding error and you you need smaller number? Use float.

Comments

No Comments Yet.
Be the first to tell us what you think.