C# float, double range, precision, definition and type conversion

Lionsure 2020-07-19 Original by the website

I. C# float(Single precision)

Floating-point types are divided into single-precision and double-precision. float is single precision, used to define 32-bit floating-point(decimal) variables. The alias is System.Single, the value range is -3.4 × 1038 to 3.4 × 1038, and the precision is 7 Bit, occupying 4 bytes and 32 bits.


II. C# double(Double precision)

double is a double-precision floating-point type, used to define a 64-bit floating-point(decimal) variable, alias is System.Double, the value range is ±5.0 × 10-324 to ±1.7 × 10308, the precision is 15-16 bits, occupying 8 byte and 64 bits.


III. C# float definition variable instance

float f = 2;

float f1 = 2.0f;//Without f, it means double type

float f,n;//The default value is 0.0f


float f = 1, n = 2.2f;

float[] arr = { 2, 6.5f, 12 };//float array



IV. C# double definition variable instance

double d = 2;

double d = 2d;//Add d, the integer is treated as double

double d = 2.0;


doube d,n;//The default value is 0.0d

double d = 1D, n = 2.2;

double[] arr = { 5, 6.5, 12d };//double array



V. C# floating point type conversion

1. Convert float to double

float f = 2;

double d = f;//Implicit conversion

double d = Convert.ToDouble(f);


2. Convert double to float

double d = 2.2;

float f = Convert.ToSingle(d);

Convert double to float. Implicit conversion cannot be used, otherwise an error occurs.


3. Convert C# float, double to string

float f = 2.5f;

string s = Convert.ToString(f);


double d = 2.2;

string str = Convert.ToString(d);


To convert to other data types, the method is similar to the above(that is, use the corresponding method of Convert), learn from analogy.