First, it is clear that float and double of the floating point type in Java cannot be used for capital calculation.
This principle is also mentioned in objective Java: float and double can only be used for scientific computing or engineering computing, and Java. Math. bigdecimal is used in commercial computing.
It is not difficult to apply it in practice. A few notes:
1. Database Table Design
The data type of the fund field is generally defined as number, and the length can be determined as needed.
2. Definitions of Data Types of Po attributes in hibernate
There are two options: double and float are recommended for precision loss. In. HBM. XML, which is automatically generated by hibernate ing tool, the default type is double.
3. Calculation of funds in the program
Java. Math. bigdecimal must be used instead of float or double. It is recommended to write a tool class and provide some common methods for addition, subtraction, and other operations.
Instance reference:
Public class basecalculate {Private Static final int def_div_scale = 10; /*** provides an exact addition operation * @ Param V1 addend * @ Param V2 addend * @ return and */public static double add (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.add (B2 ). doublevalue ();} /*** provides the exact subtraction operation * @ Param V1 is reduced * @ Param V2 is reduced * @ return is the difference between the two parameters */public static double substract (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.subtract (B2 ). doublevalue ();} /*** provides exact Multiplication operation * @ Param V1 multiplier * @ Param V2 multiplier * @ return product of two parameters */public static double multiply (double V1, double V2) {bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.multiply (B2 ). doublevalue ();}/*** provides (relatively) Precise Division operations. In case of division, * is accurate to 10 digits after the decimal point, and the digits after the decimal point are rounded down. * @ Param V1 divisor * @ Param V2 divisor * @ return two parameter vendors */public static double divide (double V1, double V2) {return divide (V1, V2, def_div_scale);}/*** provides (relatively) accurate Division operations. * When the Division is not complete, the scale parameter determines the precision, and the subsequent numbers are rounded down. ** @ Param V1 divisor * @ Param V2 divisor * @ Param scale indicates the number of digits after the decimal point * @ return parameter operator */public static double divide (double V1, double V2, int scale) {If (scale <0) {Throw new illegalargumentexception ("the scale must be a positive integer or zero");} bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return b1.divide (B2, scale, bigdecimal. round_half_up ). doublevalue ();} /*** provides precise decimal scale rounding processing * @ Param V requires rounding numbers * @ Param scale number after decimal point number reserved * @ return rounding result */public static double round (Double V, int scale) {If (scale <0) {Throw new illegalargumentexception ("the scale must be a positive integer or zero");} bigdecimal B = new bigdecimal (double. tostring (v); bigdecimal one = new bigdecimal ("1"); return B. divide (one, scale, bigdecimal. round_half_up ). doublevalue ();} public static void main (string [] ARGs) {// you can directly use a floating point number for calculation. The result is a problematic system. out. println (0.01 + 0.05); // After the bigdecimal class is used for computation, the system can be accurately calculated. out. println (basecalculate. add (0.0000000000005, 0.00000001 ));}}