Pages

Saturday, November 12, 2011

Format Number in Java


Formatting number is something that u might always require in scenarios like, when you are trying to create a sequence number or a unique ticket number, math calculations that would require scientific notations etc. Every technology would have a provision for this. The one found is java is sophisticated.

// The 0 symbol shows a digit or 0 if no digit present
NumberFormat formatter = new DecimalFormat("000000");
String s = formatter.format(-1234.567);  // -001235
// notice that the number was rounded up

// The # symbol shows a digit or nothing if no digit present
formatter = new DecimalFormat("##");
s = formatter.format(-1234.567);         // -1235
s = formatter.format(0);                 // 0
formatter = new DecimalFormat("##00");
s = formatter.format(0);                 // 00


// The . symbol indicates the decimal point
formatter = new DecimalFormat(".00");
s = formatter.format(-.567);             // -.57
formatter = new DecimalFormat("0.00");
s = formatter.format(-.567);             // -0.57
formatter = new DecimalFormat("#.#");
s = formatter.format(-1234.567);         // -1234.6
formatter = new DecimalFormat("#.######");
s = formatter.format(-1234.567);         // -1234.567
formatter = new DecimalFormat(".######");
s = formatter.format(-1234.567);         // -1234.567
formatter = new DecimalFormat("#.000000");
s = formatter.format(-1234.567);         // -1234.567000


// The , symbol is used to group numbers
formatter = new DecimalFormat("#,###,###");
s = formatter.format(-1234.567);         // -1,235
s = formatter.format(-1234567.890);      // -1,234,568

// The ; symbol is used to specify an alternate pattern for negative values
formatter = new DecimalFormat("#;(#)");
s = formatter.format(-1234.567);         // (1235)

// The ' symbol is used to quote literal symbols
formatter = new DecimalFormat("'#'#");
s = formatter.format(-1234.567);         // -#1235
formatter = new DecimalFormat("'abc'#");
s = formatter.format(-1234.567);         // -abc1235

String pre=”xx”;
String post=”yy”;
formatter = new DecimalFormat(pre+"'abc'000000"+post);
s=formatter.format(12.3);           // xxabc000012yy

In addition to this you can also print formatted output ot the console by using System.out.format command
System.out.format("%5s", Integer.toString(n));  //     n  (- including 5 white spaces)

Hope this helps J

No comments:

Post a Comment