This question already has answers here:
How many significant digits do floats and doubles have in java?
(6 answers)
Closed 7 years ago.
I have a problem when it comes to calculating pi with more than 15 decimal places(i used double).
My result looks quite good except my variable is limited to 15 decimal places:
3.140592653839794
Anybody could tell me what i have to do if i want more decimal places?
Thanks and Greeting!
Use java.math.BigDecimal instead of double for arbitrary (finite) precision.
Related
This question already has answers here:
BigDecimal setScale and round
(2 answers)
Set specific precision of a BigDecimal
(5 answers)
Closed 1 year ago.
I am trying to write a program that has a step in it where I would like to divide two numbers and get a decimal number to 60 places.
For instance, I would like to divide 1 by 17 and get 0.016393442622950819672131147540983606557377049180327868852459 without losing any precision.
I am trying to store the number in a BigDecimal but I am having trouble finding a good way to accomplish this without losing the precision after the 16th digit or so
This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.
This question already has answers here:
How to do a fractional power on BigDecimal in Java?
(3 answers)
Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it? [closed]
(3 answers)
Closed 8 years ago.
BigDecimal was the choice to store numbers that have up to 5 digits after decimal point. I need to raise the BigDecimal to fractional power (up to 2 digits after decimal point).
For example I have to bring 9.09671 to power of 1.51. Do I need some conversions from/to BigDecimal? How to do it?
EDIT:
I cannot use 3rd party libraries as described in Java's BigDecimal.power(BigDecimal exponent): Is there a Java library that does it?
Is there more elegant, succint way for this case than described in How to do a fractional power on BigDecimal in Java?
This question already has answers here:
How can I handle precision error with float in Java?
(9 answers)
Closed 8 years ago.
some calculations with doubles return the wrong result.
E.g.
System.out.println(""+(0.05+0.01));
output
0.060000000000000005
What can I do to correct this error?
Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 9 years ago.
I retrieve some data from a database but the number has a lot of digits and I want to round it. The thing is I do not want the whole number to be rounded.
Example :
I draw number 4.1010359882326385E10 which is of type long.
I want to round it to 4.10103598823264.
That is to round the number after the 15th digit.
Any way I can do this?
E10 in your number is a scientific notation, that says you number is actually 41010359882.326385. If you transform it to 4.10103598823264, you are not doing a rounding, you are doing something else.