JAVA BigDecimal decimal point processing, javabigdecimal
1. retain two decimal places
Method 1: {double c = 3.154215; java. text. DecimalFormat myformat = new java. text. DecimalFormat ("0.00"); String str = myformat. format (c );}
Method 2: {java. text. decimalFormat df = new java. text. decimalFormat ("#. 00 "); df. format (the number you want to format); example: new java. text. decimalFormat ("#. 00 "). format (3.1415926 )#. 00 indicates two decimal places #. 0000 four decimal places and so on ...} method 3: {double d = 3.1415926; String result = String. format ("%. 2f "); %. 2f %. indicates the number of digits before the decimal point. 2 indicates that the result of two decimal places is f indicating the floating point type} rounded to {double f = 111231.5585; BigDecimal B = new BigDecimal (f ); // retain 2 decimal places double f1 = B. setScale (2, BigDecimal. ROUND_HALF_UP ). doubleValue ();}
Public class PreciseCompute {// default division operation precision: private static final int DEF_DIV_SCALE = 10;/*** provides precise addition operations. * @ Param v1 add count * @ param v2 add count * @ 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 precise subtraction. * @ Param v1 subtrahend * @ param v2 subtrahend * @ return Difference Between Two Parameters */public static double sub (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. * @ Param v1 multiplier * @ param v2 multiplier * @ return product of two parameters */public static double mul (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, the Division is accurate to * 10 digits after the decimal point, and the subsequent digits are rounded down. * @ Param v1 divisor * @ param v2 divisor * @ return two parameter vendors */public static double div (double v1, double v2) {return div (v1, v2, DEF_DIV_SCALE);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines * the precision, and the number is rounded down. * @ Param v1 divisor * @ param v2 divisor * @ param scale indicates the number of digits after the decimal point. * @ Return operator of two parameters */public static double div (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 rounded decimal places. * @ Param v the number to be rounded off * @ param scale the number of digits to be retained after the decimal point * @ return the result after rounding */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 ne = new BigDecimal ("1"); return B. divide (one, scale, BigDecimal. ROUND_HALF_UP ). doubleValue ();}}
private BigDecimal formatComma2BigDecimal(Object obj) {String val = String.valueOf(obj);if (val == null)return new BigDecimal("0.00");val = val.replaceAll(",", "");if (!isNumber(val))return new BigDecimal("0.00");BigDecimal decimal = new BigDecimal(val);decimal = decimal.setScale(2, RoundingMode.HALF_UP);return decimal;}private String formatCommaAnd2Point(Object obj) {BigDecimal decimal = formatComma2BigDecimal(obj);DecimalFormat df = new DecimalFormat("#,###.00");String decimalStr = df.format(decimal).equals(".00")?"0.00":df.format(decimal);if(decimalStr.startsWith(".")){decimalStr = "0"+decimalStr;}return decimalStr;}private boolean isDouble(String value) {try {Double.parseDouble(value);if (value.contains("."))return true;return false;} catch (NumberFormatException e) {return false;}}private boolean isInteger(String value) {try {Integer.parseInt(value);return true;} catch (NumberFormatException e) {return false;}}private boolean isNumber(String value) {return isInteger(value) || isDouble(value);}