Solved

Double.parseDouble Difficulties

Posted on 2000-04-11
3
323 Views
Last Modified: 2010-05-18
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?

harry.willis##sssltd.co.uk
(where ## is @)
0
Comment
Question by:harrywillis
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 25 total points
ID: 2703898
Well, i've compiled a simple program

public class DoubleTest
{
    public static void main(String[] args)
    {
        System.out.println(Double.parseDouble(args[0]));
    }
}

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)?

Michael
0
 

Author Comment

by:harrywillis
ID: 2703988
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.
0
 

Author Comment

by:harrywillis
ID: 2704194
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now