Advertisement
advictoriam

Untitled

Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.    This program reads a price in dollars and cents,
  5.    rounds the price to the nearest dime,
  6.    and prints out the revised price.
  7. */
  8. public class NearestDime
  9. {
  10.    public static void main(String[] args)
  11.    {
  12.       System.out.print("Please enter the price: ");
  13.       Scanner in = new Scanner(System.in);
  14.       double price = in.nextDouble();
  15.       int pennies = (int) Math.round(price * 100);
  16.  
  17.       // Determine dollar and cents worth of pennies
  18.       int cents;
  19.       int dollars;
  20.  
  21.       dollars = (int)(price);
  22.       cents = (int)((price - (double)(dollars)) * 100);
  23.  
  24.       // Round cents to nearest dime
  25.       int dimes;
  26.      
  27.       dimes = (int)(Math.round((double)(cents)/10));    
  28.  
  29.       // Print revised price
  30.       double revised = dollars + dimes * 0.1;
  31.       System.out.printf("%.2f\n", revised);      
  32.    }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement