Results 1 to 4 of 4
  1. #1
    Cass29 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Format Specifiers

    Hi everyone,
    I hope someone can help me. I'm writing a program for a college assignment that needs to handle numbers in millions like 5,550,000. I'v declared the variable to hold them as type long, but the only way I can get the numbers into a printf statement is to use the format specifier %.2f. This works fine but the only problem is the number has 2 decimal points that I dont want to be there because they will always be 0's. Does anyone know if there is a specific format specifier for long as %d wont work either.
    Thank you.

  2. #2
    gcampton Guest

    Default

    printf ( " %d ", longValue);

    also you might want to consider BigIntegers.
    Last edited by gcampton; 12-20-2009 at 12:44 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Two common ways to do this are the String.format / printf group of methods (and use as noted above %d will work for longs) or you could create a DecimalFormat instance. Both ways will allow strings to be shown with or without groupings:

    Java Code:
    import java.text.DecimalFormat;
    
    public class Fu1 {
      public static void main(String[] args) {
        long myLong = 1234567890123L;
    
        DecimalFormat dfWithGrouping = new DecimalFormat("00");
        dfWithGrouping.setGroupingSize(3);
        dfWithGrouping.setGroupingUsed(true);
        System.out.print("With Grouping: ");
        System.out.println(dfWithGrouping.format(myLong));
        
        DecimalFormat dfWithOutGrouping = new DecimalFormat("00");
        System.out.print("Without Grouping: ");
        System.out.println(dfWithOutGrouping.format(myLong));
        
        System.out.println();
        System.out.printf("With Grouping: %,d %n", myLong);
        System.out.printf("Without Grouping: %d %n", myLong);
      }
    }

  4. #4
    Cass29 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Thanks

    Thanks for the help guys my code is working perfectly now.

Similar Threads

  1. JSP code format
    By joeyxaza in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-20-2009, 06:11 PM
  2. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  3. Date Format
    By learnspring in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 06:16 PM
  4. Format
    By 2ndis1stplaceloser in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 07:38 AM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 04:37 AM

Tags for this Thread

Posting Permissions

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