jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes double validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double validation" Watch "double validation" New topic

double validation

Christian Long

Joined: Oct 04, 2007
Posts: 28
Hello 6hr00 here,I have not sleep yet..

Anyway, I have a 'cost' attribute & I have declared it as 'double'.

The fact is that all 'costs' generally comes in 452.50(integer.2decimalplaces). Is it possible to cast the double values to "2 decimal places'? If yes, please give me some hint or a link to some webpage.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

Doubles don't represent decimal values so well as they can be stored differently and cause rounding errors. For example, 3.33333333333333333 doesn't equal 3.33. In practice, people usually use BigDecimal which does let you set the number of decimal places.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Christian Long

Joined: Oct 04, 2007
Posts: 28
ok, Thanks - I'm looking at BigDecimal, I just learned something more..
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
They use BigDecimal, or they use either int or long and store the value of 452.50 as 45250. When you do this, you need to make sure it's well-documented what the numbers mean. E.g. does a field value represent dollars, or cents? But then again, you always need to document that sort of information.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: double validation
It's not a secret anymore!