• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double with only two decimals?

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And these days you can also use String.format and its relatives like System.out.printf.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that helped!

It works now
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic