Convert from double to BigDecimal in Java

0

I have a query ... I have the following code that generates a Pi number of 16 digits ... but I require 20 digits, so I thought it would be convenient to use the bigDecimal but I still do not know how to enter it. help (?

Code:

package io2;

import java.math.BigDecimal;

public class MetodoBuffon {
    public static void main(String[] args){
        double longitud = 4.4;
        double x1;
        double x2;
        double a;
        double b;
        double corta= 0;
        double total = 100000000;
        double n;
        for (int i = 0; i < total; i++) {
            a= -1 + Math.random() *2;
            b = -1 + Math.random() *2;
            x1= Math.random();
            x2 = x1 +((a* longitud)/ (Math.pow((Math.pow(a, 2)+ Math.pow(b, 2)), 0.5)));
            n= 0;
            for (int j = 0; j < 1+ (1/longitud); j++) {
                if(x1 < n && n< x2){
                    corta++;
                    break;
                }
                if(x1 >n && n> x2){
                    corta++;
                    break;
                }
                n = n + longitud;
            }
        }
        System.out.println("corta: "+corta);
        double pi = (2* total)/corta;
        System.out.println("pi: "+pi);
    }
}
    
asked by Marjorie Garcia 18.05.2018 в 04:09
source

1 answer

1

You can do the following (I have commented on the solution):

Code:

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class SO {

    public static void main(String[] args) {

        double longitud = 4.4;
        double x1;
        double x2;
        double a;
        double b;
        double corta = 0;
        double total = 100000000;
        double n;

        for (int i = 0; i < total; i++) {
            a = -1 + Math.random() * 2;
            b = -1 + Math.random() * 2;
            x1 = Math.random();
            x2 = x1 + ((a * longitud) / (Math.pow((Math.pow(a, 2) + Math.pow(b, 2)), 0.5)));
            n = 0;

            for (int j = 0; j < 1 + (1 / longitud); j++) {
                if (x1 < n && n < x2) {
                    corta++;
                    break;
                }
                if (x1 > n && n > x2) {
                    corta++;
                    break;
                }
                n = n + longitud;
            }

        }

        System.out.println("corta: " + corta);
        double pi = (2 * total) / corta;

        // Paso 1. BigDecimal
        BigDecimal numeroLargo = new BigDecimal(pi);
        System.out.println("\nBigDecimal..........");
        System.out.println("Número largo: " + numeroLargo);

        // Paso 2. Obtener solo 20 decimales del "numeroLargo"
        DecimalFormat numero20Decimales = new DecimalFormat("#.####################");
        System.out.println("\nDecimalFormat..........");
        System.out.println("pi: " + numero20Decimales.format(numeroLargo));

    }
}

Result:

BigDecimal..........
Número largo: 3.34834407141696477339110060711391270160675048828125

DecimalFormat..........
pi: 3.34834407141696477339

I hope I have helped you, regards!

    
answered by 18.05.2018 / 04:32
source