How to declare a real variable with at least 20 digits in Java

1 Answer

0 votes
import static java.math.RoundingMode.HALF_UP;
import java.math.BigDecimal;
import java.math.MathContext;

public class Main {
    public static void main(String[] args) {
        String pi = "3.141592653589793238462643";
        
        MathContext m = new MathContext(25, HALF_UP);
        BigDecimal result = new BigDecimal(pi, m);

        System.out.println(result);
    }
}


/*
run:

3.141592653589793238462643

*/


 



answered Jun 26, 2025 by avibootz

Related questions

1 answer 87 views
1 answer 85 views
1 answer 91 views
1 answer 81 views
1 answer 73 views
1 answer 196 views
...