wood burning stoves*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double validation" Watch "double validation" New topic
Author

double validation

Christian Long
Greenhorn

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Christian,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Christian Long
Greenhorn

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

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: double validation
 
Similar Threads
converting float to double
using the math class for rounding numbers
Why am I not getting the right answer
I need help!!!
Why 100 * A * B * C is not same as A * B * C * 100 if A, B, C are double?