Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Decimal precision after division in JAVA
Decimal precision after division in JAVA
The problem you are talking about involves the problem of digital accuracy. In programs, digital operations are always automatically converted from low precision to high precision. For example, (int)/(float) will get the number of decimal places you want, which is determined by the operation mode of numbers in the computer. If you use 3/5.0 or 3.0/5, you can get the decimal you want.

In a computer, the expressions of integer (int) and float, double) are different, and the occupied memory space is also different. In operation, in order to get the correct result, it needs to be aligned, and if the data types are inconsistent, it needs to be converted. On the hardware level, it is to convert an integer that occupies less memory into a floating point, that is, to add extra bytes for this integer to participate in the operation, although these bytes are all 0, which is a problem of digital accuracy.

Now there are two integers. When operating, the computer thinks that there is no need to convert data types, so it gets the results you see.