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

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?
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Not sure your question makes much sense - to me at any rate.
0
Commented:
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));
}
}
``````
0
Author Commented:
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
Commented:
double can only go so far. Consider
`````` double pi = Math.PI; // prints   3.141592653589793
``````
But, we all know pi is a  Non-terminating decimal.
0
Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks a lot.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.