Java bigdecimal decimal point Processing

Source: Internet
Author: User

Keep 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 any number of digits before the decimal point. 2 indicates that the result of two decimal places is f indicating the floating point type.
}

}

Rounded {

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 ();}}

 

My code:

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);}

For more information, see the javase help documentation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.