#56 – How to Round When Converting Float to Int

When converting from float or double to int, we cannot use an implicit cast, because data would be lost.  We need an explicit cast, as shown below.  When casting from float or double to int, the resulting value is truncated (rounded towards 0) to get an integral result.

 float f1 = 4.8f;
 //int n1 = f1;        // Compile-time error: cannot implicitly convert
 int n1 = (int)f1;     // Explicit cast, truncated, n1 = 4

If you want to round to the nearest integer, rather than truncating, you can use one of the methods in System.Convert.

 n1 = Convert.ToInt32(4.8f);   // Rounded, n1 = 5

The Convert method always rounds to the nearest integer, unless the result is halfway between two integers.  In this case, it rounds to the nearest even integer.

 n1 = Convert.ToInt32(8.5f);  // Rounded to nearest even, n1 = 8
 n1 = Convert.ToInt32(9.5f);  // Rounded to nearest even, n1 = 10
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #56 – How to Round When Converting Float to Int

  1. kai zhou says:

    Nice summary, thank you Sean.

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 )

Connecting to %s

%d bloggers like this: