JAVA BigDecimal decimal point processing

Source: Internet
Author: User

Keep two decimal places {

Method One: {

Double c=3.154215;

Java.text.DecimalFormat myformat=new Java.text.DecimalFormat ("0.00");

String str = Myformat.format (c);
}


Way two: {

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 represents two decimal places #.0000 four decimal places and so on ...
}


Way three: {

Double d = 3.1415926;

string result = string. Format ("%.2f");

%.2f%. Indicates that any number of digits before the decimal point 2 indicates that the result of the two-bit decimal format is F for floating-point
}

}


Rounded

Double F = 111231.5585;
BigDecimal B = new BigDecimal (f);
Keep 2 decimal places
Double f1 = B.setscale (2, bigdecimal.round_half_up). Doublevalue ();
}

 Public classPrecisecompute {//Default division operation PrecisionPrivate Static Final intDef_div_scale = 10; /*** provides accurate addition operations. * @paramv1 summand *@paramV2 Addend *@returntwo parameters of A and*/     Public Static DoubleAddDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnB1.add (B2). Doublevalue (); }    /*** provides accurate subtraction operations. * @paramv1 minuend *@paramv2 *@returnthe difference of two parameters*/     Public Static DoubleSubDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.subtract (B2). Doublevalue (); }    /*** provides accurate multiplication operations. * @paramv1 by multiplier *@paramV2 Multiplier *@returnproduct of two parameters*/   Public Static DoubleMulDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.multiply (B2). Doublevalue (); }    /*** Provide (relative) accurate division operations, when there are no more than an endless situation, accurate to * after the decimal 10 digits, after the number rounded. * @paramV1 Dividend *@paramv2 Divisor *@returntwo parameters of the quotient*/     Public Static DoubleDivDoubleV1,Doublev2) {     returnDiv (v1, v2, Def_div_scale); }    /*** Provide (relative) accurate division operations. When the exception occurs, the scale parameter refers to the fixed precision, and the subsequent numbers are rounded. * @paramV1 Dividend *@paramv2 Divisor *@paramthe scale representation needs to be accurate to several points after the decimal point. * @returntwo parameters of the quotient*/   Public Static DoubleDivDoubleV1,DoubleV2,intScale ) {     if(Scale < 0) {      Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.divide (B2, scale, bigdecimal.round_half_up). Doublevalue (); }    /*** Provides precise rounding of decimal digits. * @paramv need to round the number *@paramA few * after the decimal point@returnresults after rounding*/   Public Static DoubleRoundDoubleVintScale ) {     if(Scale < 0) {      Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal b=NewBigDecimal (double.tostring (v)); BigDecimal NE=NewBigDecimal ("1"); returnb.divide (one, scale, bigdecimal.round_half_up). Doublevalue (); }  }

My Code:

PrivateBigDecimal formatcomma2bigdecimal (Object obj) {String val=string.valueof (obj); if(val = =NULL)              return NewBigDecimal ("0.00"); Val= Val.replaceall (",", "" "); if(!Isnumber (val))return NewBigDecimal ("0.00"); BigDecimal decimal=NewBigDecimal (Val); Decimal= Decimal.setscale (2, ROUNDINGMODE.HALF_UP); returndecimal; }      PrivateString formatcommaand2point (Object obj) {BigDecimal decimal=formatcomma2bigdecimal (obj); DecimalFormat DF=NewDecimalFormat ("#,###.00"); String Decimalstr= Df.format (decimal). Equals (". 00")? " 0.00 ":d F.format (decimal); if(Decimalstr.startswith (".") ) {Decimalstr= "0" +Decimalstr; }          returnDecimalstr; }      Private Booleanisdouble (String value) {Try{double.parsedouble (value); if(Value.contains ("."))                  return true; return false; } Catch(NumberFormatException e) {return false; }      }      Private BooleanIsinteger (String value) {Try{integer.parseint (value); return true; } Catch(NumberFormatException e) {return false; }      }      Private Booleanisnumber (String value) {returnIsinteger (value) | |isdouble (value); }

See the Javase help documentation for details.

Go from: Java Journey

JAVA BigDecimal decimal point processing

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.