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!
You can try using Math.round(number)
And you may need to type cast it to turn it into an int.
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.
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.
``` 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.
Join our real-time social learning platform and learn together with your friends!