Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting fields

Status
Not open for further replies.

bobo12

Programmer
Dec 26, 2004
74
US
hi,
in java, how do we format fields. for instance, suppose i want a double to print upto 3 decimal #'s, how do we specify this.
i want to do this for other data types as well, int, etc...
 
Have a look at the java.text.NumberFormat and java.text.DecimalFormat classes.

Tim
 
Alternatively, if you have access to Tiger then you can start using more advanced formatting functionality with the inbuilt Formatter class :

The Formatter class also has the ability to make it much easier to read formatted input.

Usage

Formatter myFormatter=new Formatter()
foratter.format("This %s is %d characters","String",6);

Prints

This String is 6 characters

So if you know C and have used its printf(...) methods, you should find the Formatter class quite easy to use.

One excellent feature of the Formatter class is that it allows you to specify argument indexes. This has the added benefit where you do not need to specify an argument twice to have 2 or more formatting options applied to it :-

formatter.format("%s in uppercase is %1$S",hello);

prints :

hello in uppercase is HELLO

 
That's really curious.

After all, here it comes the 1.5 thingie to take us back again to C.

Btw, has anyone already tested it in the real world?

Cheers,

Dian
 
Yes I have used Tiger for 6 months, the Formatter class along with the Scanner class is well implemented and Tiger is soooooooooooooo fast !
 
I've heard that 1.5 is quicker than 1.4, but frankly didn't believe it could be, unless they have rewritten the JIT compiler/interpreter, which I thought they had not - is it really quicker in your opinion ?

Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
Well, I've just read about it, not tested, but they improved the performace changing garbage collection, class sharing, thread priorities and some new no synchornized classes.

I guess that it can be faster if you rewrite your code to take full advantage of the new features. That's why I'ma asking, does it worth the work?

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top