I am using java.util.Random to generate a random gaussian. I need to convert this gaussian to a float value. However gaussian is a double, so I need some way to either round then convert it to a float. I need to round to the nearest whole number, rounding up. Here is my question: How?
In order to round float and double numbers in Java, we use the java. lang. Math. round() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With