File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes double with only two decimals? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double with only two decimals?" Watch "double with only two decimals?" New topic
Author

double with only two decimals?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi

Is ther a way to make sure that a double value can have max two decimals?

Thanks in advance
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Double values have arbitrary precision, but when you display a value, you can format it however you like, including limiting the number of decimal places that display. Check out, for example, the java.text.DecimalFormat class.

[Jess in Action][AskingGoodQuestions]
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

And these days you can also use String.format and its relatives like System.out.printf.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Ernest Friedman-Hill wrote:Double values have arbitrary precision, but when you display a value, you can format it however you like, including limiting the number of decimal places that display. Check out, for example, the java.text.DecimalFormat class.
Are you sure you mean double? I thought doubles always had about 15-16 decimal significant figures' precision, unless they have a magnitude less than their lowest normalised value. Did you mean BigDecimal has any precision specified, and it can be specified at 2 decimal places.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Campbell Ritchie wrote:Are you sure you mean double? I thought doubles always had about 15-16 decimal significant figures' precision,


Of course you're correct, but I was speaking non-technically. I just meant "arbitrary" in the sense of "not user specified" or "not controllable." The OP asked if you could somehow constrain a double to have two decimal places, and I said no, the number of decimal places is not something you can control. But poor choice of words on my part, because "arbitrary precision" has a technical meaning.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Thank you Ernest
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
If i want to use the format method, how do i do that.

I've already looked in the API, but I don't understand it
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Hava you checked java.util.Formatter? That specifies the start quite early - %f. Later on, it shows you how to specify the number of decimals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Rob Prime wrote:Hava you checked java.util.Formatter?
And the Formatter methods can be called indirectly with String#format, or printf, here in the Java™ Tutorials or similar.
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thanks, that helped!

It works now
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double with only two decimals?