OneBite.Dev - Coding blog in a bite size

convert variable from float to int in java

Code snippet on how to convert variable from float to int in java

float number = 9.7; 
int convert = (int) number;

This code converts an float variable, ‘number’, to an int variable, ‘convert’. The float variable ‘number’ is initialized to 9.7. By typecasting this float to an int, the float is converted to an int and stored in the ‘convert’ variable. The typecasting is done by adding “(int)” before the ‘number’ variable. So, in this case, the integer value 9 is stored in the ‘convert’ variable.

java