This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Issue with the Parse Float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Issue with the Parse Float" Watch "Issue with the Parse Float" New topic
Author

Issue with the Parse Float

Jyothish Divakar
Greenhorn

Joined: Feb 12, 2007
Posts: 10
Hi,

I am using the following code to find a difference between two floating integers in string format and returns a string as well.

public String floatDifference(String value1, String value2) {

float diffValue = 0.0f;
String retVal = "";
diffValue = Float.parseFloat(value1)
- Float.parseFloat(value2);
retVal = Float.toString(diffValue);
return retVal;
}

This method is giving me a precision of 4 digits in the result. For instance I passed "2510.00" and "856.32" and it returns "1653.6799" but when I pass "2510.00" and "856.52" and it returns "1653.48". I am using J2SE version 1.4.

Can anyone help in investigating on why is this precision get changed for different inputs?

Regards,
Jyothish
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
You might want to read this on floating point accuracy.


Ping & DNS - my free Android networking tools app
Vilmantas Baranauskas
Ranch Hand

Joined: Dec 20, 2006
Posts: 89
Not sure if you are dealing with money but somewhere I've seen a very good example of how to deal with money in OO-way.

The idea is to have special Money class with methods add(Money), subtract(Money). Then you write myPocket.add(new Money(...)).

Internally you may store money depending on requirements. One option would be to store amount*100.

In addition, you can introduce easily currency support later if needed.


Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
Indeed, float and double should not be used for monetary calculations - use the BigDecimal class instead.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The original poster doesn't say that the calculations involve money. The two-decimal-place requirement does suggest money, though.

Using floating-point (float or double) for money calculations is a seriously bad idea. Don't do it!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jyothish Divakar
Greenhorn

Joined: Feb 12, 2007
Posts: 10
Hi all,

Thanks for your speedy response. i used the NumberFormat and got the solution for the digit issue. but really i wanted to know why is this different behaviour to different sets of data of same kind.

Thanks,
Jyothish
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Go read the link Ulf gave you above. The short answer is that some real numbers simply can't be represented exactly in floating point.
[ April 28, 2008: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
 
GeeCON Prague 2014
 
subject: Issue with the Parse Float