Results 1 to 2 of 2
  1. #1
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with Format.justify method

    Hello. I am trying to round a double to the nearest Integer, and display the answer in a DoubleField for a GUI application.
    I've tried to cast the variable being used to type int, and tried using the Format.justify method. Here's the code, any help would be appriciated.

    Java Code:
    /**
    *Calculates the population growth
    **/
    
    import javax.swing.*;
    import BreezySwing.*;
    import BreezySwing.Format;
    public class PopulationGrowth extends GBFrame
    {
    	private JLabel initialOrganismsLabel;
    	private JLabel growthRateLabel;
    	private JLabel hoursLabel;
    	private JLabel growthHoursLabel;
    	private DoubleField initialOrganismsField;
    	private DoubleField growthRateField;
    	private DoubleField hoursField;
    	private DoubleField growthHoursField;
    	private DoubleField totalOrganismsField;
    	private JButton calculateButton;
       
    //*************************Constructor Method***********************************
       public PopulationGrowth()
       {
       	initialOrganismsLabel = addLabel  ("Initial Organisms" ,1,1,1,1);
       	initialOrganismsField = addDoubleField 	(0.0 ,1,2,1,1);
       	growthRateLabel = addLabel  ("Growth Rate" ,2,1,1,1);
       	growthRateField = addDoubleField  (0.0 ,2,2,1,1);
       	hoursLabel = addLabel ("Hours to achieve this rate" ,3,1,1,1);
       	hoursField = addDoubleField	(0.0 ,3,2,1,1);
       	growthHoursLabel 	= addLabel  ("Hours to grow" ,4,1,1,1);
       	growthHoursField 	= addDoubleField 	(0.0 ,4,2,1,1);
       	totalOrganismsField = addDoubleField 	(0 ,5,1,2,2);
       	calculateButton = addButton ("Calculate Population" ,6,2,1,1);
       }
    
       public void buttonClicked (JButton buttonObj)
       {
       		double 	initialOrganisms,
       	  			growthRate,
       	  			hours,
       	  			growthHours,
       	  			growPeriod,
       	  			totalOrganisms = 0;
       	  			
       		if (buttonObj == calculateButton)
    	  	{
    	  		initialOrganisms= initialOrganismsField.getNumber();
    	  		growthRate = growthRateField.getNumber();
    	  		hours = hoursField.getNumber();
    	  		growthHours = growthHoursField.getNumber();
    	  		growPeriod = hours / growthHours;
    	  		totalOrganisms = (int)initialOrganisms * Math.pow(growthRate,growPeriod);
    	 		double temp = Format.justify('l' ,totalOrganisms, 25, 0);
    	  		totalOrganismsField.setNumber(temp);
    	  	}
       }
       				
    	public static void main (String[] args)
       	{
          PopulationGrowth GUI = new PopulationGrowth();							//Instantiate the GUI window
          GUI.setSize (500, 400);													//Set the size of the program
          GUI.setVisible (true);													//Shows the program
       	}
    }
    Thanks.

  2. #2
    mathias is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Have you tried adding .5 then converting to an Int?
    Java Code:
    totalOrganisms = (int)((initialOrganisms * Math.pow(growthRate,growPeriod))+.05);
    This method will do the math first, then round. Since converting to an Int from a double just cuts off everything after the decimal, adding .5 will "round up" correctly.
    Greetings.

Similar Threads

  1. DateField in format : HH:mm:ss:ms
    By gibsonsydsa in forum CLDC and MIDP
    Replies: 4
    Last Post: 03-10-2008, 08:46 AM
  2. Timestamp in SQL format
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 12:37 PM
  3. Code format
    By Eric in forum XML
    Replies: 1
    Last Post: 07-06-2007, 08:44 AM
  4. Code format
    By Eric in forum XML
    Replies: 1
    Last Post: 07-06-2007, 08:42 AM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 04:37 AM

Posting Permissions

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