C# basic ②-data type (the difference between decimal and float, double)

 

                       

 

1. Actual combat drill:

int number1 = 12;                              //一个整数类型的变量并赋值
double number2 = 1.11111111111111111111;       //64位双精度浮点型数据(存小数时电脑默认)
float number3 = (float)1.11111111111111111111; //32位单精度浮点型数据(使用时需先进行转换:0.1f或(flost)0.1)
char number4 = 'a';                            //字符数据,‘a’
string number5 = "123";                        //字符串数据,“abc“

Console.WriteLine(number1);
Console.WriteLine(number2);
Console.WriteLine(number3);
Console.WriteLine(number4);
Console.WriteLine(number5);
Console.ReadKey(); 

Output result:

           


 

2. Let me ask you a question

? ? Double and float both store decimals. Why do they need to be divided into two? One is not enough. What is the difference between the two?

Let's first look at an example:

double number2 = 1.11111111111111111111;
float number3 = (float) 1.11111111111111111111;

Console.WriteLine(number2);
Console.WriteLine(number3);
Console.ReadKey(); 

Output result:

            

 

According to the value range of double and float:

double 64-bit double precision floating point (+/-)5.0 x 10^-324 to (+/-)1.7 x 10^308
float 32-bit single precision floating point -3.4 x 10^38 to + 3.4 x 10^38

 

They are represented by pictures like this:

                    

double has a larger representation range than float


 

Three ? ? Ask questions

Question 1: When assigning values ​​to two variables, isn't there a long string of numbers? Why are these only a few of the output results?

Question 2: The declared number3 is single-precision floating-point data. Why do you need to write ( float ) when assigning a value later ? What is its function? Can't it?


Four, solve

Question 1 :

Through the code at the time of declaration and the output result, we can find:

When assigning a value to the declared double type number2 variable , there are 20 digits after the decimal point, but the output result is only 16 digits

When assigning a value to the declared number3 variable of type float , there are 20 digits after the decimal point, but the output result is only 7 digits

In fact, in C# ,

  • double double precision, in a computer accounted . 8 bytes, a precision of 16 bit
  • folat single precision, in a computer it accounted for . 4 bytes, bits are valid . 7 bit
  • Decimal is high precision, the effective digit is 28

 

Question 2 :

Let's take a look at what the code looks like in VS2019

The system will report a red error message: "Cannot implicitly convert the Double type to the "float" type; please use the "F" suffix to create this type.

What does it mean?

In C# ,

When declaring a precision variable, the system will automatically default this decimal to a value of type double , so the number3 variable declared in the above figure now looks like a value of the float data type, but in fact it is a double type Numerical value. If you want to convert this double type to a float type, you need to add " F " after the float value or add ( float ) before the decimal . As shown below

float number3 = (float)1.11111111111111111111;

 

float number3 = 1.11111111111111111111F;

 

to sum up

In C#, double represents a larger range than float;

The number of significant digits of decimal is larger than that of double and float types, but the representation range is smaller than both of them;

Decimal is suitable for banks, which are more accurate currencies, and can be accurate fractions;

When the decimal precision type value is declared, the system will automatically default to the double type;

When declaring a float type value, you need to add (float) before the decimal or add F after the decimal

 

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/107110480