1.
String mymoney = "100.0128 ";
Bigdecimal money = new bigdecimal (mymoney );
// Set the precision and rounding rules
Money = money. setscale (2, bigdecimal. round_half_up );
System. Out. println (money );
// 100.01
2. Double mymoney = 100.0128;
Mymoney = math. Round (mymoney * 100)/(double) 100;
Bigdecimal money = new bigdecimal (mymoney );
System. Out. println (money); // There are many decimals following it
// To ensure the number of decimal places is 2 digits
Bigdecimal money = new bigdecimal (double. tostring (mymoney ));
System. Out. println (money); // The number of decimal places is 2 digits.
This class is really useful. It was found on the Internet by a female Java programmer. Amazing ~~~~~~ Haha
/* <Br/> * creation date: 2004-10-14 <br/> * If exact calculation is required, you must use string to create bigdecimal. <br/> */<br/> package COM. LIMs. actions. testqc. comm; <br/>/** <br/> * @ author jstar <br/> * WINDOW> preferences> JAVA> code generation> code and annotations <br/> */<br/> Import Java. math. bigdecimal; <br/>/** <br/> * Because Java's simple type cannot accurately perform operations on floating point numbers, this tool provides precision <br/> * floating point number operations, including addition, subtraction, multiplication, division, and rounding. <Br/> */<br/> public class Arith {<br/> // default Division calculation precision <br/> Private Static final int def_div_scale = 10; <br/> // This class cannot be instantiated <br/> private Arith () {<br/>}< br/>/** <br/> * provides precise addition operations. <Br/> * @ Param V1 add count <br/> * @ Param V2 add count <br/> * @ return and <br/> */<br/> Public static double add (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); </P> <p> return b1.add (B2 ). doublevalue (); <br/>}< br/>/** <br/> * provides precise subtraction. <Br/> * @ Param V1 subtrahend <br/> * @ Param V2 <br/> * @ return <br/> */<br/> Public static double sub (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.subtract (B2 ). doublevalue (); <br/>}< br/>/** <br/> * provides exact multiplication. <Br/> * @ Param V1 multiplier <br/> * @ Param V2 multiplier <br/> * @ return product of two parameters <br/> */<br/> Public static double MUL (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.multiply (B2 ). doublevalue (); <br/>}< br/>/** <br/> * provides (relatively) Precise Division operations. When division is not complete, accurate to <br/> * 10 digits after the decimal point, and rounded up after the decimal point. <Br/> * @ Param V1 divisor <br/> * @ Param V2 divisor <br/> * @ return two parameter vendors <br/> */<br/> Public static double Div (double V1, double V2) {<br/> return Div (V1, V2, def_div_scale); <br/>}< br/>/** <br/> * provided (relative) precise Division operations. In case of division, the scale parameter indicates <br/> * precision, and the number is rounded down. <Br/> * @ Param V1 divisor <br/> * @ Param V2 divisor <br/> * @ Param scale indicates the number of digits after the decimal point. <Br/> * @ return operator of two parameters <br/> */<br/> Public static double Div (double V1, double V2, int scale) {<br/> If (scale <0) {<br/> throw new illegalargumentexception ("the scale must be a positive integer or zero "); <br/>}< br/> bigdecimal B = new bigdecimal (double. tostring (v); <br/> bigdecimal one = new bigdecimal ("1"); <br/> return B. divide (one, scale, bigdecimal. round_half_up ). doublevalue (); <br/>}