This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The extra (right ) precision comes from the fact that Java floating point types are IEEE754 floating-point types. To use the terminology of IEEE (if I remember erectly), a floating-point type is an "inexact representation of a real number".
Thanks for the quick reply: the issue is this value is going to be displayed as currency, how am I to deal with it rounding up if all I know about the input is that it's decimal is .4? Do I need to write my own work around for forcing the value to round down?
Personally, I prefer Marilyn's suggestion. Assuming you are using dollars as the currency value, you can use an int to hold the number of cents instead. Then for the final output divide by 100 to get the dollar amount and use the % operator to get the decimal amount.
I hope this description is clear enough, but if you still need help, don't be afraid to post some code so we can help you further.
Originally posted by Layne Lund: Personally, I prefer Marilyn's suggestion.
Agreed - unless you have to work with fractions of cents, which many financial applications have to...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jan 11, 2002
Thanks for all the answers. The context for this question was as such; I was retrieving this numneric value from a bean with a property of type float. I was then throwing it into a jstl fmt tag to get the proper format. This is where I was getting the unpredictable results. So I switched the type of the bean property from float to String and this worked. Thanks.
Originally posted by Ilja Preuss: ... unless you have to work with fractions of cents, which many financial applications have to...
Yes, this is the context I was coming from. The financial calculations I've worked with are not simple dollars and cents, but involve intermediate steps requiring up to 15 digits to the right of the decimal.