6 Replies Latest reply on Aug 23, 2011 4:56 PM by 796440

    Double to string giving extra values

      hi all,

      i have encountered a issue in java and the issue is --

      if my decimal no is 0.001 to 0.009 and when i am converting double to string

      it is giving string as 0.0010 to 0.0090

      the following code is what i am trying to do

      double d1 = 0.006;
      System.out.println("d===>"+ d1);
      String temp1= String.valueOf(d1);
      System.out.println("temp1===>"+ temp1);

      if(temp1.indexOf(".")!=-1 && (temp1.substring(temp1.indexOf(".")+1).length()>3)){
      System.out.println("yes in the if condition");
      System.out.println("no in the else part");

      due to the requirement i cannot use Bigdecimal or decimal formater as we are doing validation operation on the resulting string.

      can any one suggest any solution for that or any work around this problem ???

      and is this a java issue ???
        • 1. Re: Double to string giving extra values
          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.
          • 2. Re: Double to string giving extra values
            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.
            • 3. Re: Double to string giving extra values
              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 ????
              • 4. Re: Double to string giving extra values
                Bug ID: 4428022
                in google and u will find the bug
                • 5. Re: Double to string giving extra values
                  My problem is not what the defination of floating point
                  That 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 suits
                  Don't put words into my mouth. That is a stupid statement and I did not make it.
                  • 6. Re: Double to string giving extra values
                    880996 wrote:
                    so according to u, java is adding any arbit value it suits fit
                    That is absolutely not what he said and absolutely not what is happening.

                    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