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: Format Precision Exception? What's going on?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Format Precision Exception? What's going on?

    public class test {
        public static void main(String[] args) {
           double meter;
           double foot;
           double x = 1;
           double y = 20;
     
     
            System.out.println("Feet \t Meters \t | \t Meters \t Feet");
     
            while (x <= 10 && y <= 65){
                meter = footToMeter(x);
            foot = meterToFoot(y);
                System.out.printf("%10.1d \t %10.3d  | %10.1d \t %10.3d", x, meter, y, foot);
     
     
            x++;
            y += 5;
            }//end of while
     
        }//end of main method
     
        public static double footToMeter(double foot) {
     
            return 0.305 * foot;
        }//end of footToMeter
     
        public static double meterToFoot(double meter) {
     
           return 3.279 * meter;
        }//end of meterToFoot
     
     
     
    }

    it is something inside this part, but i can't figure out what is wrong:
    System.out.printf("%10.1d \t %10.3d  | %10.1d \t %10.3d", x, meter, y, foot);


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Format Precision Exception? What's going on?

    Use "%d" for ints and longs.
    Use "%SomethingElse" for floats and doubles.

    Formatter syntax


    Cheers!

    Z

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

    ColeTrain (October 28th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Format Precision Exception? What's going on?

    Thanks! Works perfect now. I figured d stood for double, good to know. I appreciate the fast and informative response!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Format Precision Exception? What's going on?

    d is for decimal because, in that case, we are dealing with formatting an int using its decimal (base 10) representation.

Similar Threads

  1. Number Format Exception
    By alexz003 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 16th, 2011, 09:22 AM
  2. Number Format Exception while parsing long
    By Aamir in forum Java Networking
    Replies: 1
    Last Post: May 19th, 2011, 03:59 AM
  3. Possible Loss of Precision
    By Canadian_Pirate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2011, 01:41 AM
  4. Possible loss of precision (double/int)
    By haloboy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 02:23 AM
  5. set Precision
    By sabir in forum Java Theory & Questions
    Replies: 1
    Last Post: March 4th, 2011, 11:32 AM