wood burning stoves 2.0*
The moose likes Java in General and the fly likes Float value weirdness... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Float value weirdness..." Watch "Float value weirdness..." New topic
Author

Float value weirdness...

Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Guys,

Please explain this:


This prints out: 178493.40625! Where does the extra (wrong) precision come from?

Thanks.


SCJP 1.4 SCJD 1.5
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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".

On the topic, do you know of any banks whose account software uses floating-point types?
http://qa.jtiger.org/GetQAndA.action?qids=59&showAnswers=true


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
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?

Thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you're dealing with currency, you should use BigDecimal instances rather than floating-point primitives.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
or multiply by 100 and use ints until you get the final answer.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne


Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Folks,

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.

- M
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float value weirdness...