Assign variables to numbers left of decimal and right

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 49erfan21
    New Member
    • Feb 2009
    • 1

    Assign variables to numbers left of decimal and right

    Edit: Ah, never mind. I feel silly. I just needed to convert amount into an integer.
    I am having a little trouble figuring out how to assign a variable (dollars) to the numbers left of the decimal and how to assign a variable (cents) to the right of the decimal. The only parts of the code I can modify are those 2 assignment statements because it is a homework question. The user inputs something like 123.45

    Thanks for any help.

    Code:
    import java.util.Scanner;
    public class ApplicationSkel
    {
    
      public static void main(String[] args)
      {
        double amount;
    	 int dollars, cents;
    	 Scanner scan = new Scanner(System.in);
    	 
    	 System.out.print("Enter your purchase $");
    	 amount = scan.nextDouble();
    	 
    	 [B]dollars = ________;
    	 cents  = ________;[/B]
    	 
    	 System.out.print("You entered " + dollars + " dollars and " + cents + "."); 
    
      }
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    try casting amount to an int?

    Comment

    Working...