This content has been marked as final. Show 6 replies
It is doing exactly what it is supposed to do, and it is all due to your inane 'requirement' that you 'cannot use Bigdecimal or decimal formatter' which would solve the problem.
You need to have a good look at What every computer scientist should know about floating point, starting with the realization that double d = 0.006 does not have an exact representation. If you want to represent 0.006 exactly in your program text, you must represent it as new BigDecimal("0.006"). No ifs or buts about it.
If you have a specific requirement to only produce three decimal places you need to specify this. Otherwise double.toString() will convert the double as it does.
I don't see why you cannot use BigDecimal or DecimalFormatter as these are often used for validation. (There is a recent question on this subject) I can only assume this is homework. I also assume this doesn't have anything to do with collections. ;)
If you get one more digit than you want, you can remove it.
BTW: Validation like this are not fool proof. The string ".!$&" would pass your test.
My problem is not what the defination of floating point ,
it is when i convert string to double and then again double to string.
java is adding extra 0 when it is taking a value in Double.
so according to u, java is adding any arbit value it suits fit and there is no workaround this issue other than using BigDecimal or any other number formater ????
My problem is not what the defination of floating pointThat is exactly your problem. When 0.006 is converted to a double the value is actually more like 0.00600000000000000012490009027033011079765856266021728515625. Then you get some bad rounding.
The real question is why you're converting exact literals to doubles and back to Strings and expecting the same result. I quote from the bug report you cited: 'In general, it should not be expected that System.out.println(fp_literal) returns the same string as that which comprises fp_literal'. This is not how normal computer programs are written. If you know the value, use it. If the value arises from a computation it probably won't be exactly 0.006 in the first place and if it is and you want an exact decimal representation you should use the facilities provided by the language.
so according to [you], java is adding any arbit[rary] value it suitsDon't put words into my mouth. That is a stupid statement and I did not make it.
880996 wrote:That is absolutely not what he said and absolutely not what is happening.
so according to u, java is adding any arbit value it suits fit
First, are you aware that a double cannot hold the value 0.1? And do you know why? And are you aware that this is not Java-specific, but rather is an expected and accepted consequence of the IEEE 754 standard that is used not only by Java but by C, C++, and a host of other languages?
Second, if I understand your problem, it's with the formatting of the String representation. If you wish to change that, then use java.util.Formatter or java.text.DecimalFormat. (Yes, I know, you said you can't do that. Ridiculous requirement that shows a lack of understanding of computers and programming.)
Edited by: jverd on Aug 23, 2011 9:53 AM