Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert floating-point numbers into integers in Java and keep only the integer part?
How to convert floating-point numbers into integers in Java and keep only the integer part?
By converting to a string and then truncating the integer part:

Floating point a =1.1f; //define a floating-point variable a.

string str = string . value of(a); //the floating-point variable a is converted into the string str.

int idx = str.lastIndexOf(" . "); //Find the position of the decimal point

String strNum = str.substring(0,idx); //Intercept the string as an integer from the beginning of the string to the decimal point.

int num = integer . value of(strNum); //Convert the integer part into a number through the Integer.valueof method.

Extended data:

There are two types of floating points in Java: float and double.

Java floating-point types have fixed table number range and field length, and the field length and table number range have nothing to do with the machine. Double type represents double precision floating point number, and float type represents single precision floating point number.

Java floating-point numbers follow the IEEE754 standard and are expressed by scientific binary data counting methods. For floating-point values, 1 bit is the sign bit, the next 8 bits represent the exponent, and the next 23 bits represent the mantissa. For even numbers, the first bit is also the sign bit, the next 1 1 bit represents the exponent, and the next 52 bits represent the mantissa.

Baidu Encyclopedia -Java (Computer Programming Language)

Baidu Encyclopedia-Floating Point Number (Rational Number)