Java BigDecimal divide and get digit to N places [duplicate] - java

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

Related

Format a float to 2 decimal places without changing variable type [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.

How to round to 1 decimal place using Java [duplicate]

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.

Remove Decimal from BigDecimal [duplicate]

This question already has answers here:
Java BigDecimal remove decimal and trailing numbers
(6 answers)
Closed 6 years ago.
I have a BigDecimal number, I just want to remove the decimal numbers from it,
for example if I have 200.88 then output should be 200?
I tried Bigdecimal rounding function but they wont do the job
You can specify the rounding mode to ROUND_FLOOR when you use round.

Java - 100 decimal places in variable [duplicate]

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.

java rounding numbers [duplicate]

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.

Categories

Resources