How to round double to nearest whole number and then convert to a float?

The Solution to How to round double to nearest whole number and then convert to a float? is


float b = (float)Math.ceil(a); or float b = (float)Math.round(a);

Depending on whether you meant "round to the nearest whole number" (round) or "round up" (ceil).

Beware of loss of precision in converting a double to a float, but that shouldn't be an issue here.

~ Answered on 2011-11-22 22:58:25


Most Viewed Questions: