Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Double to string giving extra values

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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");
    }
    else
    {
    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 ???


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Double to string giving extra values

    java double tostring extra zero - Google Search
    Bug ID: 4428022 System.out.println(0.001) outputs 0.0010

    I'm not overly familiar with Java bug reports, but I see a reference to '7' in the 'Release Fixed' field which makes me suspect this is still current.

    A kludge would be:
    System.out.println(Double.toString(0.006d).replaceFirst("0*$", ""));

Similar Threads

  1. Searching for string values in a circular list
    By hippo1974 in forum Object Oriented Programming
    Replies: 10
    Last Post: July 11th, 2011, 04:36 AM
  2. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  3. Extracting the " (Double Quote) character from a string
    By jai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 28th, 2011, 08:57 AM
  4. [SOLVED] Found String Requires Double Problem.
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 01:24 PM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM