This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

decimal format

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Cata.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
while(!flag)
{
line = data.readLine();
if (line==null) flag=true;
else
{
String temp=""+frm.format(i++)+": "+line;
datao.write(temp,0,temp.length());
datao.newLine();
}
}
datao.close();
data.close();

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).]
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++)
System.out.println(leftPad(Integer.toString(data[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. )

______1
_234567
____453
_____-8
_____55

*/


}
 
J R Quigley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Help.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be this (I have not checked boundary values) would do the trick if you don't want the commas:

SCJP2 (1.4) Wannabe
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic