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 2 of 2

Thread: How to format?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to format?

    Here is the code:

    import java.util.Scanner;
     
    public class Problem2 {
        public static void main (String []args){
     // Scanner
            Scanner scan = new Scanner(System.in);
     
     // Declorations
            double kilograms = 1;
     
     
            System.out.println("Kilograms\tPounds");
     
    // For loop
          for (int i = 1; i <= 199; i += 2) {
     
     System.out.println(i + "                " + i * 2.2);
     
        }
     
    }
     
     }

    The answers give me two columns, but the problem is sometimes there are 10 zeros after the decimal so for the third answer it is 6.6000000000 etc. I just want to use one character after the decimal and I don't know how. I have tried the printf and using %d,f etc but that didn't work :/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: How to format?

    How to Format a double to 2 decimal places

Similar Threads

  1. [SOLVED] For loop-ing decimal format
    By Harj in forum Loops & Control Statements
    Replies: 10
    Last Post: October 1st, 2010, 05:47 PM
  2. [SOLVED] Help with code, Says number is NaN and i cant format it
    By NewGuy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2010, 07:28 PM
  3. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 10:30 AM
  4. Sting format give more truble
    By ahmethbaai in forum Java Theory & Questions
    Replies: 1
    Last Post: December 17th, 2009, 12:19 PM
  5. help with text format codes
    By vanchan09 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 03:12 PM