Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- This program reads a price in dollars and cents,
- rounds the price to the nearest dime,
- and prints out the revised price.
- */
- public class NearestDime
- {
- public static void main(String[] args)
- {
- System.out.print("Please enter the price: ");
- Scanner in = new Scanner(System.in);
- double price = in.nextDouble();
- int pennies = (int) Math.round(price * 100);
- // Determine dollar and cents worth of pennies
- int cents;
- int dollars;
- dollars = (int)(price);
- cents = (int)((price - (double)(dollars)) * 100);
- // Round cents to nearest dime
- int dimes;
- dimes = (int)(Math.round((double)(cents)/10));
- // Print revised price
- double revised = dollars + dimes * 0.1;
- System.out.printf("%.2f\n", revised);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement