All pastes #461070 Raw Edit

Miscellany

public text v1 · immutable
#461070 ·published 2007-04-27 14:23 UTC
rendered paste body
/ This is a test of BigDecimal to calculate pi (eventually).
import java.math.*;

public class Bigtest{
    public static void main(String[] args){
        int z = 1;
        BigDecimal k = new BigDecimal(Integer.toString(z));
        // Section 1: (1 / Math.pow(16,k))
        double x = 16;
        BigDecimal sixteen = new BigDecimal(Double.toString(x));
        BigDecimal sixteen2 = new BigDecimal(Double.toString(x));
        for(int i=1;i<5;i++){
            sixteen = sixteen.multiply(sixteen2);
        }
        BigDecimal one = new BigDecimal("1");
        BigDecimal section1 = new BigDecimal(one.toPlainString());
        section1 = section1.divide(sixteen);
        System.out.println(section1.toPlainString());
        // Section 2
        BigDecimal eight = new BigDecimal("8");
        BigDecimal four = new BigDecimal("4");
        BigDecimal six = new BigDecimal("6");
        BigDecimal five = new BigDecimal("5");
        // Section 2a (4/(8*k + 1))
        BigDecimal sec2abottom = new BigDecimal(eight.toPlainString());
        sec2abottom = sec2abottom.multiply(k);
        sec2abottom = sec2abottom.add(one);
        BigDecimal sec2a = new BigDecimal("4");
        sec2a = sec2a.divide(sec2abottom);  // This returns an inexact value, i.e. 4/9
        System.out.println(sec2abottom.toPlainString()); // How do I round it to n decimal places?
        
    }
}