Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Formatting this number.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Formatting this number.

    i would like this tiny bit of help.

    i built this little program that calculates stuff(unimportant what it calculates).

    but sometimes when its dividing or multiplying big numbers, it displays it in the scientific notification.
    like it should display: 10000000(10 million)
    but it displays: 1.0E7.

    how can i fix this? i dont want it to display it in the scientific notifitcation.

    code:
    if(e.getSource()==b1 && box.getSelectedItem()==fish[8]){
    				String xp1 = txt1.getText();
    				int xp2 = Integer.parseInt(xp1);
    				String xp3 = txt2.getText();
    				int xp4 = Integer.parseInt(xp3);
    				double xpleft = xp4 - xp2 ;
    				double fishleft = xpleft / 350;
    				JOptionPane.showMessageDialog(null, "XP left: "+"\t"+xpleft+"\n"+box.getSelectedItem()+" left: "+"\t"+fishleft+"\n"+"\n"+"NOTE: always round these numbers."+"\n"+"example: fish left: 1,87 = fish left: 2.", "Fishing Calculator", JOptionPane.INFORMATION_MESSAGE);


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Formatting this number.

    Format the string. There is a format() method for this. The options available in the format string are described in the Formatter class.

    public class FormatEg {
        public static void main(String[] args) {
            double big = 12345678.9;
            String toDisplay = String.format("A big number: %.0f", big);
            System.out.println(toDisplay);
        }
    }

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    MR bruto (June 10th, 2013)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Formatting this number.

    Also at j-f.org: i just can't fix this

  5. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting this number.

    Please try BigDecimal as well:
    double big = 123456789.6;
    System.out.println((new BigDecimal(Double.toString(big))).toPlainString()) ;
    //output:
    //123456789.6

Similar Threads

  1. formatting issues help
    By Zomby in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2012, 03:28 PM
  2. Gson Formatting
    By techwiz24 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:48 PM
  3. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 07:41 PM
  4. Replies: 3
    Last Post: August 19th, 2009, 12:30 PM