• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double validation

 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, Thanks - I'm looking at BigDecimal, I just learned something more..
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic