jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes decimal format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "decimal format" Watch "decimal format" New topic

decimal format

cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
Hi all,
I used DecimalFormat to count the lines in a file, but the output is something like 001, 002, ..., 011, 012 and so on. What should I do in order to have the output like __1, __2 ... and so on, with spaces , of course.
Cheers and thanks to all of you.
Tom Patrick
Ranch Hand

Joined: Oct 24, 2000
Posts: 30
What method are you using to format your numbers? I have some code that does the following:

Which gives me a number that is formatted properly with comma's EX: 1,345,231
Does that help at all?
[This message has been edited by Tom Patrick (edited December 20, 2000).]
cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
Hi Tom,
Have a look at this.

File f = new File(args[0]);
FileInputStream fis = new FileInputStream(f);
BufferedReader data = new BufferedReader(new InputStreamReader(fis));

File fo = new File(args[0]+"bak");
FileOutputStream fos = new FileOutputStream(fo);
BufferedWriter datao = new BufferedWriter(new OutputStreamWriter(fos));

DecimalFormat frm = new DecimalFormat("000");

boolean flag=false;
int i=1;
String line;
line = data.readLine();
if (line==null) flag=true;
String temp=""+frm.format(i++)+": "+line;

Of course, everything goes in main(String[] args). Try it and then we talk more.
Anyway, thank you for taking the time to answer.
Cata Lin.
[This message has been edited by cata lin (edited December 21, 2000).]
[This message has been edited by cata lin (edited December 21, 2000).]
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
If you just want to have spaces if there is a 0 you need to apply a different pattern in the DecimalFormat.
I think
DecimalFormat frm = new DecimalFormat("##0");
is what you want. I'm not sure if that puts in spaces or nothing - if not, you'll have to test the length of the string and add 0's at the front accordingly.
J R Quigley

Joined: Aug 01, 2002
Posts: 2
I am having the same problem as that posted by Cata Lin. I am trying to list an integer with a fixed number of positions on the screen (such as three) where leading zeroes appear as spaces. I tried the following formats:
DecimalFormat df1 = new DecimalFormat("###,##0");
DecimalFormat df3 = new DecimalFormat("##0");
However what I get instead are simply truncated positions so that numbers don't line up on the right when the output goes from two to three digits, for example.
I've searched the Sun Java API and also the Java Ranch archives and I cannot find a clear solution to this problem.
Is there a clear and simple way to do this?
Norm Miller
Ranch Hand

Joined: May 21, 2002
Posts: 56
class leading {

public static void main(String[] args) {
int data[] = { 1, 234567, 453, -8, 55 };

// List out data on screen
for ( int i = 0; i < data.length; i++)

/* pad out the string to width. Put the spaces on the left */
private static String leftPad(String r)
{ return repeat(' ', width - r.length()) + r;

private static String repeat(char c, int n)
{ if (n <= 0) return "";
StringBuffer s = new StringBuffer(n);
for (int i = 0; i < n; i++) s.append(c);
return s.toString();
static int width = 7; // Desired column width

/* Result is (I replaced the blanks by underscores because the web page "loses" the spacing. )



J R Quigley

Joined: Aug 01, 2002
Posts: 2
Neat program. But surely there must be a shorter way to replace leading zeroes in a number with printing blanks, something like Cobol does with PIC ZZZ,ZZ9.
Should we use DecimalFormat or some other class to do this?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
May be this (I have not checked boundary values) would do the trick if you don't want the commas:

SCJP2 (1.4) Wannabe

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: decimal format
It's not a secret anymore!