Note that if the result is an infinite number of decimal places, you will get an error if you do not specify
Import Java.util.scanner;import Java.math.bigdecimal;public class Main {public static void main (String args[]) {Scanner in = new Scanner (system.in); BigDecimal a = In.nextbigdecimal (); BigDecimal B = in.nextbigdecimal (); System.out.println ("Add" + a.add (b)); System.out.println ("Sub" + a.subtract (b)); System.out.println ("Mul" + a.multiply (b)); System.out.println ("Divide" + a.divide (b));//This assumes that the result is an infinite loop. You will get an error System.out.println ("Rounding:" + a.divide (b,5,bigdecimal.round_half_up));//The second parameter is the number of decimal digits a.divide (b,5, BIGDECIMAL.ROUND_HALF_UP). doublevalue;double D1 = 3.1415926; BigDecimal T1 = new BigDecimal (D1); The doubletobigdecimal conversion int d2 = 3214856 can be performed sequentially; BigDecimal t2 = new BigDecimal (D2); BigDecimal t3 = new BigDecimal ("843269136"); System.out.println ("Dobule to:" + T1); System.out.println ("Int to:" + T2); System.out.println ("String to:" + T3);}}
Java Decimal class and rounding method with very high precision decimal time with