Results 1 to 4 of 4
Thread: Format Specifiers
- 12-20-2009, 11:17 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
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.
- 12-20-2009, 11:42 AM #2gcampton Guest
printf ( " %d ", longValue);
also you might want to consider BigIntegers.Last edited by gcampton; 12-20-2009 at 11:44 AM.
-
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); } }
- 12-20-2009, 03:49 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
JSP code format
By joeyxaza in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-20-2009, 05:11 PM -
how to convert one format to another format
By mahipal_reddy621 in forum New To JavaReplies: 1Last Post: 12-02-2008, 10:21 AM -
Date Format
By learnspring in forum New To JavaReplies: 1Last Post: 11-16-2008, 05:16 PM -
Format
By 2ndis1stplaceloser in forum New To JavaReplies: 2Last Post: 10-21-2008, 06:38 AM -
show a RTF FORMAT
By Jack in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks