I need to calculate dollars and cents on output but i dont now how to do it like the example I can only calculate all the money
Then they enter how many quarters, cents, niquel,etc, they have then i use this to have quantityTotalDollar = (int) quantityTotal % 100; to calculate the money and cents but i dont know how to calculate only coins and display it like this
You have 1 dollar(s) and 26 cent(s).
ok I think I understand now: you are given X cents and want to find out how many dollars and cents there are.
Let's say
int totalCoins = 234;
int dollars = totalCoins/100;
int cents = totalCoins - (dollars*100);
System.out.println("you have $ "+dollars + " and " + cents +" cents ");
Here we create the "int" variable dollars which can only be whole numbers. In this case this is rounded to 2.
Then we create cents by taking the total number : 234 then taking away (2*100) which leaves 34.
To calculate the number of dollars + the number of cents, use this expression
money = (dollars) + (cents/100);
I assume that you want to divide dollars by cents though, so the following should work.
money = (dollars) / (cents/100);
I do not completely understand the question, so just remember, 1 dollar = 100 cents.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.