Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Why is the decimal part omitted when C# floating-point display is converted to integer?
Why is the decimal part omitted when C# floating-point display is converted to integer?
The C# floating-point display is rounded when converted to an integer, which will force the conversion to discard the decimal part.

This assignment will store the value of the operation result in the stack into a variable. In this process, the actual value should be X = 70074.438+088, while the value stored in C is c=70075.0 because of the accuracy problem.

Example:

{

floata = 70.075f

intbb =(int)(a * 1000);

}

{

floata = 70.075f

float c = a * 1000;

doublex = a * 1000;

intcc =(int)c;

}

Extended data

Precautions for use

The precision of a single-precision floating-point number is only about 7 digits (decimal). If multiplied by 1000, the error will be amplified by 1000 times. Then the floating-point number is directly truncated into int32 numbers. For such a wide range of algorithms, this loss of accuracy is normal, and finally only 4 is regarded as a significant number in the value of int32).

Compiled code of. Net is different in different system versions and different CPU. And the processing of instructions by different CPUs is definitely not exactly the same. Even. Net2.0framework, released with the current software package 1 1, 12 years ago and 20 15 years ago, I believe the code compiled by Jit is different. People think. Net in recent years, it can improve its accuracy slightly on the basis of ensuring its operating efficiency.