Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

Hello, I am trying to write a java method for calculating total annual salary. The method receives monthlySalary and monthlySales. The commission is 2% of monthly sales. The output has to be an int rounded to the nearest dollar. I can't seem to get it working and output "1245" instead of "1244.63" Please help!

OpenStudy (ankit042):

You can try using Math.round(number)

OpenStudy (e.mccormick):

And you may need to type cast it to turn it into an int.

OpenStudy (anonymous):

Thanks, I'll give it a try. I was type casting the double into an int before, but it was rounding to the nearest 1000, ie 12000 from 12120.

OpenStudy (e.mccormick):

The easy way it to have it return the cast of the rounded number. So it takes in the salary and sales, does the math, and returns a rounded number that is cast into an int.

OpenStudy (e.mccormick):

``` public class RoundTest{ public static void main(String[] args){ double a = 12120.682; System.out.println("Just a: " + a); System.out.println("Int a: " + (int)a); System.out.println("Rounded a: " + Math.round(a)); System.out.println("Int of Rounded a: " + (int)Math.round(a)); } } ``` Got me: Just a: 12120.682 Int a: 12120 Rounded a: 12121 Int of Rounded a: 12121 The Rounded one might return an int, but by casting it after the rounding, you make sure it returns an int.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!