Solved

how print out all digits after decimal point for a variable (data type double) in java?

Posted on 2014-07-27
6
765 Views
Last Modified: 2014-07-29
Would like to print out max digits for a variable (data type double). For example,
double d1 = 100.01234567890123456789012345678901234567890123456789
double d2 =  2.0
How to print out d1/d2, which still keeps exact answer?

Another one:
d1 = 1.0; d2 = 3.0
How to print out the digits of d1/d2 as many as possible for this data type?
0
Comment
Question by:jl66
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40222692
Not sure your question makes much sense - to me at any rate.
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 40222785
Please run this code.
import java.math.BigDecimal;
import java.math.MathContext;
public class TestD {

   public static void main(String args[]){
        double d1 = 100.01234567890123456789012345678901234567890123456789;
        double d2 = 2.0; 
        double d3 = 1.0;
	double d4 = 3.0;
        System.out.println(d1/d2);
        System.out.println(d3/d4);
	BigDecimal bd1 = new BigDecimal(100.01234567890123456789012345678901234567890123456789);
	BigDecimal bd2 = new BigDecimal(2.0);
	BigDecimal bd3 = new BigDecimal(1.0);
	BigDecimal bd4 = new BigDecimal(3.0);
	System.out.println(bd1.divide(bd2));
        System.out.println(bd3.divide(bd4,MathContext.DECIMAL128));
   }
}

Open in new window

0
 

Author Comment

by:jl66
ID: 40223290
Thanks for the tip.
If BigDecimal is not used, there is anything we can do with "double" to test the digit limit for "double" after the decimal point? The reason is that many math functions only deal with "double", but not BigDecimal.  We want to keep the precision as high as possible.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 40223499
double can only go so far. Consider
 double pi = Math.PI; // prints   3.141592653589793

Open in new window

But, we all know pi is a  Non-terminating decimal.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 250 total points
ID: 40227076
double has a precision of 15 or 16 significant decimal digits. significant means it is regardless where the decimal point is. you can test that by printing the d1 of your first sample. you will see that the output would differ from input with the 16th or 17th digit.

the precision is not always the same because a double is not stored as a decimal but in binary format as a pair of mantissa and exponent (where both have a sign bit).  

you could store your values as biginteger what would increase the precision to 20 - 21 digits. that could make sense if you always know where the decimal point is which must be placed later for output.

for higher precisions you would need an array of some basic type and use algebraic methods for the arithmetical operations.

Sara
0
 

Author Closing Comment

by:jl66
ID: 40228222
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
basic hardware to learn oop advanced design patterns 3 88
groupSumClump challenge 9 98
Which is the latest version of Apache Tomcatl 3 50
use lov values 2 24
Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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 …

911 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

21 Experts available now in Live!

Get 1:1 Help Now