So I was building a program where I take different coins and print out the value into dollars and cents. Since I needed each to be a whole number I did some coding to make it so. My problem is that on certain instances, when running the program, my cents value comes out wrong. EVERYTHING else is correct. I'm assuming this is something to do with converting to an integer and its causing it not to round when certain user input is given. For example, when I enter 10 quarters, 3 dimes, 6 nickels, and 2 pennies, it comes out with 11 cents at the end. I can not figure out the error here.
My full code is below. Can anyone let me know what I did wrong?
public static void main (String[]args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of quarters: ");
double qs=sc.nextDouble();
System.out.print("Enter the number of dimes: ");
double ds=sc.nextDouble();
System.out.print("Enter the number of nickels: ");
double ns=sc.nextDouble();
System.out.print("Enter the number of pennies: ");
double ps=sc.nextDouble();
System.out.print("\n");
double qV=0.25;
double dV=0.10;
double nV=0.05;
double pV=0.01;
double tQ=qs*qV, tD=ds*dV, tN=ns*nV, tP=ps*pV;
double totalV=tQ+tD+tN+tP;
int dollars=(int)totalV;
double cent=(totalV-dollars)*100;
int cents=(int)cent;
int tqs=(int)qs;
int tds=(int)ds;
int tns=(int)ns;
int tps=(int)ps;
System.out.println("You entered "+tqs+" quarters.");
System.out.println("You entered "+tds+" dimes.");
System.out.println("You entered "+tns+" nickels.");
System.out.println("You entered "+tps+" pennies.");
System.out.print("\n");
System.out.println("Your total is "+dollars+" dollars and "+cents+" cents.");
}