Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# double validation

Christian Long
Greenhorn
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
Marshal
Posts: 35095
380
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
ok, Thanks - I'm looking at BigDecimal, I just learned something more..

Jim Yingst
Wanderer
Sheriff
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.