• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double to string giving exta values

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


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 ???
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use NumberFormat and put a restriction on the number of digits after the decimal if all you need is from .0001 to .0009.
Go through the API, you can work it out yourself.
 
sinsand singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i cannot use numberformater or any other formater, i want to validate the value depending upon the no of charaters.
as the String.valueof(double) is giving the wrong value of length on which i have to work on.

any other way other than any formater ???

and is this a java Issue ???
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't make sense. Using String.valueOf is using a formatter (look at the Javadoc description for Double.toString() for details of how it works). Since it's using a formatter that doesn't behave the way you want, the solution is to use one that does. The only question is whether you can work out settings to get the result you want (and DecimalFormat is pretty flexible), or whether you have to write your own from scratch.

A value like 0.006 isn't stored precisely as a double. So, given that the real value will be something like 0.0059999736498632408972 (approx) you need to somehow specify the way in which this should be converted into a string. It's not a Java issue, it's a "storing a floating point value in binary" issue.

What is the actual rule you are trying to validate?
 
sinsand singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HI all
it is a java bug and here is its location

http://bugs.sun.com/view_bug.do?bug_id=4428022

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

if there is any workaround this Please let me know ...
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sinsand singh wrote:http://bugs.sun.com/view_bug.do?bug_id=4428022


From the link you have pasted

Port fix from OpenJDK 6.
Posted Date : 2009-10-15 23:22:28.0



Which version of JDK are you using??? I am using JDK 6 and it is not appending any extra 0's when I tried running it
 
sinsand singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am using jdk4 in our project and client is not agree to upgrade it.

found the workaround
double tempQuantity =DoubleNumber;
if(tempQuantity<1){
tempQuantity = tempQuantity+1;
}
String temp= String.valueOf(tempQuantity);

if(temp.indexOf(".")!=-1 && (temp.substring(temp.indexOf(".")+1).length()>3)){
System.out.println("error yes");
}

adding 1 to the temporary double number if it is less than 1 and doing the validation after that

thanks a lot
 
reply
    Bookmark Topic Watch Topic
  • New Topic