Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 3.3 KB

File metadata and controls

71 lines (54 loc) · 3.3 KB
title float (C# Reference)
ms.date 07/20/2015
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
float
float_CSharpKeyword
helpviewer_keywords
float keyword [C#]
floating-point numbers [C#], float keyword
ms.assetid 1e77db7b-dedb-48b7-8dd1-b055e96a9258
caps.latest.revision 24
author BillWagner
ms.author wiwagn

float (C# Reference)

The float keyword signifies a simple type that stores 32-bit floating-point values. The following table shows the precision and approximate range for the float type.

Type Approximate range Precision .NET Framework type
float -3.4 × 1038to +3.4 × 1038 7 digits xref:System.Single?displayProperty=nameWithType

Literals

By default, a real numeric literal on the right side of the assignment operator is treated as double. Therefore, to initialize a float variable, use the suffix f or F, as in the following example:

float x = 3.5F;  

If you do not use the suffix in the previous declaration, you will get a compilation error because you are trying to store a double value into a float variable.

Conversions

You can mix numeric integral types and floating-point types in an expression. In this case, the integral types are converted to floating-point types. The evaluation of the expression is performed according to the following rules:

  • If one of the floating-point types is double, the expression evaluates to double or bool in relational or Boolean expressions.

  • If there is no double type in the expression, the expression evaluates to float or bool in relational or Boolean expressions.

A floating-point expression can contain the following sets of values:

  • Positive and negative zero

  • Positive and negative infinity

  • Not-a-Number value (NaN)

  • The finite set of nonzero values

For more information about these values, see IEEE Standard for Binary Floating-Point Arithmetic, available on the IEEE Web site.

Example

In the following example, an int, a short, and a float are included in a mathematical expression giving a float result. (Remember that float is an alias for the xref:System.Single?displayProperty=nameWithType type.) Notice that there is no double in the expression.

[!code-csharpcsrefKeywordsTypes#13]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

xref:System.Single
C# Reference
C# Programming Guide
Casting and Type Conversions
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table