#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    17
    Rep Power
    0

    How to use decimals in java


    First at all Thank you for the Help.
    I wrote a program that add prices and at the end adds 9% commision to the total, i can use 5, 10, 13 but not number with decimals like 10.29, 25.23, also the end total sum should display decimals, can you please tell me what i am missing?
    the code is:

    Code:
    import java.util.*;
    
    public class test
    {
        public static void main(String argv[])
        {
            // class , variable of class
            Scanner input = new Scanner(System.in);
            
            int total;  // declaring variable
            int price;
            int item;
            int sum;
            
            
            total = 0;
            item = 0;
            
            System.out.println("Enter price or -1 to quit: ");
            price = input.nextInt();
            
            while ( price != -1) 
            {
                total = total + price;
                item = item + 1;
                
                 System.out.println("Enter price or -1 to quit: ");
                 price = input.nextInt();
                
            }    
            
            if (total > 0)
            {
                sum =  total + (total * 9/100) ;
                System.out.printf("Total is %d\n", sum);
            }    
            else
                System.out.println("No prices were entered" );
           
        }
    }
  2. #2
  3. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    All of your variables are defined as int. Change the definition of price, sum and total to double and read nextDouble from the Scanner and it will work.
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Location
    In Front of a computer somewhere in Ontario, Canada
    Posts
    107
    Rep Power
    21
    I think I've seen something like this before...
    That -1 is familliar, there were some comments...

    Or did I dream it?
    Oh, yeah...
    *gets on topic*
    Yes, double will work just fine, like mrider said.

    BTW, I like your code, it's nice and clean, easy to read.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    17
    Rep Power
    0
    Originally Posted by mvantuyl
    All of your variables are defined as int. Change the definition of price, sum and total to double and read nextDouble from the Scanner and it will work.

    I change price, sum, and total to double, but i dont understand the nextDouble from the Scanner, can you please show me what that mean.

    thanks

    V.C.
  8. #5
  9. <- My daily commute :^)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Earth. Welcome.
    Posts
    1,501
    Rep Power
    1698
    Yes, double will work just fine, like mrider said.
    Well, actually it was mvantuyl

    Actually, I considered cautioning the O.P. about the dangers of using a double primitive: tauro27056, be aware that rounding errors can cause odd behaviour when using doubles. For a non-critical program like this I wouldn't worry too much. But as you progress, you'll find times when using doubles will cause issues. Consider the following code taken directly from page 149 of "Effective Java Programming":

    Code:
    public class TestDouble {
        public static void main( String[] args ) {
            double funds = 1.00;
            int itemsBought = 0;
            for ( double price = .10; funds >= price; price += .10 ) {
                funds -= price;
                itemsBought++;
            }
            System.out.println( itemsBought + " items bought." );
            System.out.println( "Change: $" + funds );
        }
    }
    The results should be:
    4 items bought.
    Change: $0.00

    It's actually:
    3 items bought.
    Change: $0.3999999999999999

    This is caused by rounding errors in the processor.
    Last edited by mrider; January 17th, 2006 at 02:43 PM. Reason: Accidentally submitted my message too soon.
    A -> B: Ride.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    17
    Rep Power
    0

    Question


    I change price, sum, and total to double, but i dont understand the nextDouble from the Scanner, can you please show me what that mean.


    thanks

    V.C.
  12. #7
  13. <- My daily commute :^)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Earth. Welcome.
    Posts
    1,501
    Rep Power
    1698
    Code:
            while ( price != -1) 
            {
                total = total + price;
                item = item + 1;
                
                 System.out.println("Enter price or -1 to quit: ");
                 // price = input.nextInt(); <- Change
                 price = input.nextDouble();
                
            }
    See the Scanner class API for more info here
    A -> B: Ride.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    17
    Rep Power
    0

    Talking


    Thank you for all the help, i made the changes and it took the decimals, i also made a change at the end:

    System.out.printf("Total is %.2f\n", sum);

    i replace the %d\n with %.2f\n, so my result show decimals.

    Thanks


    V. C.

IMN logo majestic logo threadwatch logo seochat tools logo