How To Convert A Float Value to An Integer

JAVA – how to convert a float value to an interger.

Example 1:

float a = 110.25;
int a = (int)(a + 0.5f);

int a will be rounded down version of float a. The value will be 110.

Example 2:

float b = 110.75;
int b = (int)(b + 0.5f);

int b will be rounded up version of float b. The value will be 111.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s