• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Double.parseDouble Difficulties

I am using:

public void setUnits(String strUnits) {
 try {
  units = Double.parseDouble(strUnits);

to convert a string contaiing a number into a double value. In the above example, units is a double.

When I run this with a string value of 2330.693064, units is assigned 2330.69 but if I input 104.797350, units is assigned 104.797. If I input 10279.169841, units is asisgned 10279.2.

This seems to suggest that the precision is determined by the number of digits in the integer value.

I want to retain the six decimal places of my source value - is there a better way to convert the string value into a double?

(where ## is @)
  • 2
1 Solution
Well, i've compiled a simple program

public class DoubleTest
    public static void main(String[] args)

and run it under JDK 1.2.2.  It seems to retain all the significant digits in the number you enter.

Why do you thing the numbers get truncated (or rounded)?

harrywillisAuthor Commented:
Using the debugger. Maybe it is just the debugger that is truncating / rounding....

I will try and add the value to my database and see what it stores.
harrywillisAuthor Commented:
Damn. The debugger in JBuilder (JB3 Enterprise Edition) was showing the truncated / rounded value.

Using System.out.prinln I found that the values were actually correct.

Thanks for your help msmolyak
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now