Is there any way to get a nth digit from BigDecimal?
Using this article --> generating pi to nth digit java
I set up a value of pi using Euler's formula in a BigDecimal. Now I would like to be able to call on a particular digit of it.
int getPiDigit(3) = 4 // 3.14
I was thinking maybe there would be a way that I could take the BigDecimal and store it's values in an array somehow. I originally tried getting the value as a String, but when it is converted to a string it only takes about 47 characters. For my use, I need to at least go to 512.
Here is some sample code to show what i'm doing...
MathContext mc = new MathContext(1000);
BigDecimal pi = new BigDecimal(20*Math.atan(1.0/7) + 8*Math.atan(3.0/79) ,mc); //Should give pi up to 1000 digits
System.out.println(pi.toString().charAt(50));
//Throws Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 50
You're computing pi as a double and then converting it to BigDecimal. That won't magically add more precision than was in the double, which by definition can't be more than 53 bits.
You need to do the computation of pi in BigDecimal.