Results 1 to 8 of 8

Thread: Problem with DIVISION

  1. #1

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158

    Problem with DIVISION

    I've written the following simple program to display a pecentage for each Pizza Chains total units but the percentage is comming out as 00.00 for all of the answers why is this? I bet it's going to be a simple mistake I made. Every thing works fine but it's to do with the division that produces the zeros any ideas?

    Code:
    import java.text.DecimalFormat;
    
    class pizzaChains
    {
        public static void main ( String args[] )
        {
    	DecimalFormat twoDP = new DecimalFormat( "00.00" );
    	
    	String chains[] = new String[10];
    	int units[] = new int[10];
    	
    	int totalUnits = 0;
    	
    	// initialise chains array with pizza chains name's
    	chains[0] = "Pizza Hut       ";
    	chains[1] = "Domino's        ";
    	chains[2] = "Little Caesar's ";
    	chains[3] = "Papa John's     ";
    	chains[4] = "Round Table     ";
    	chains[5] = "Godfather's     ";
    	chains[6] = "Chuck E. Cheese ";
    	chains[7] = "Picadilly Circus";
    	chains[8] = "Pizza Inn       ";
    	chains[9] = "Pizza Kitchen   ";
    	
    	// initialise units arrays with units for each pizza chain
    	units[0] = 14400;
    	units[1] = 5950;
    	units[2] = 4300;
    	units[3] = 1517;
    	units[4] = 539;
    	units[5] = 554;
    	units[6] = 312;
    	units[7] = 680;
    	units[8] = 514;
    	units[9] = 80;
    	
    	// calculate total units for all pizza chains
    	for ( int i = 0; i <= 9; i++ )
    	    totalUnits = totalUnits + units[i];
    	
    	// calculate & display total percentage of units for each pizza chain
    	System.out.println ( "-----------------------------------" );
    	System.out.println ( "PIZZA CHAINS    \tTOTAL UNITS" );
    	System.out.println ( "-----------------------------------" );
    	
    	for ( int i = 0; i <= 9; i++ )
    	    System.out.println ( chains[i] + "\t" + twoDP.format ( ( units[i] / totalUnits ) * 100 ) + "%" );
    	
    	System.out.println ( "-----------------------------------" );
        }   
    }
    >>> THANKS IN ADVANCE <<<

  2. #2

  3. #3

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Thanks for your post Dilenger4 but that would produce the wrong result, as that isn't how a percentage is worked out. I gave it a go any way and it did produce the incoreect results.

    Any Ideas Anyone???

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Looking at the output. The way you had it is probably the right
    way. But looking through some of my books there seems to be two format methods:

    public StringBuffer format( long number, StringBuffer result, FieldPosition fieldposition);

    public StringBuffer format(double number, StringBuffer result, FieldPosition fieldposition);

    bolth return a stringbuffer.

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I fiddled around with it and i think this would be the correct output. Tell me what you think.

    import java.text.DecimalFormat;

    class pizzaChains
    {
    public static void main ( String args[] )
    {
    //DecimalFormat twoDP = new DecimalFormat("00.00");

    String chains[] = new String[10];
    double units[] = new double[10];
    double per;
    double tu = 0.0;

    // initialise chains array with pizza chains name's
    chains[0] = "Pizza Hut ";
    chains[1] = "Domino's ";
    chains[2] = "Little Caesar's ";
    chains[3] = "Papa John's ";
    chains[4] = "Round Table ";
    chains[5] = "Godfather's ";
    chains[6] = "Chuck E. Cheese ";
    chains[7] = "Picadilly Circus";
    chains[8] = "Pizza Inn ";
    chains[9] = "Pizza Kitchen ";

    // initialise units arrays with units for each pizza chain
    units[0] = 14400;
    units[1] = 5950;
    units[2] = 4300;
    units[3] = 1517;
    units[4] = 539;
    units[5] = 554;
    units[6] = 312;
    units[7] = 680;
    units[8] = 514;
    units[9] = 80;

    // calculate total units for all pizza chains

    for ( int i = 0; i <= 9; i++ ){
    tu += units[i];
    }

    // calculate & display total percentage of units for each pizza chain
    System.out.println ( "-----------------------------------" );
    System.out.println ( "PIZZA CHAINS \tTOTAL UNITS" );
    System.out.println ( "-----------------------------------" );

    for ( int i = 0; i <= 9; i++ ){
    per = tu / units[i];
    Double d = new Double(per);
    String s = d.toString();
    int pos = s.indexOf(".");
    String sub = s.substring(0, pos + 2);
    System.out.println ( chains[i] + "\t" + sub + "%" );
    }
    System.out.println("---------------------------------");
    }

    }

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    if you ommit or comment out these following lines:

    // Double d = new Double(per);
    // String s = d.toString();
    // int pos = s.indexOf(".");
    // String sub = s.substring(0, pos + 2);

    and are left with this:


    for ( int i = 0; i <= 9; i++ ){
    per = tu / units[i];
    System.out.println ( chains[i] + "\t" + per + "%" )
    }
    System.out.println("---------------------------------");
    }

    you will end up getting:

    Pizza Hut 2.00319444444444445%
    Domino's 4.848067226890756%

    ect.... ect......
    searching a string for the "." to obtain a sub string
    was the only way i could figure out how to format the output to get the right amount of decimal places.

  7. #7
    VirtuallyVB
    Guest

    Thumbs up Integer Division versus Floating (Decimal) Point Division

    int i=1, j=3;
    (i/j == 0) not 0.3333etc.
    Try "float" for totalUnits and units[].

    Dilenger4,
    I think he's calling the inherited method
    public final String format(double number)
    from java.text.NumberFormat
    and I just noticed that you metioned integer versus floating point division.

  8. #8

    Thread Starter
    Addicted Member Ramandeep's Avatar
    Join Date
    Feb 2000
    Posts
    158
    Thanks guys!!! I changed totalUnits to float and it works, yehhhhhhh

    !!! THANKS ONCE AGAIN !!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width