Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 04:48 PM
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 07:29 AM
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
Have you tried adding .5 then converting to an Int?
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 11:49 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org