Calculate the exact amount in Java BigDecimal

Source: Internet
Author: User

Package com.chauvet.utils;

Import java.math.BigDecimal;
Import java.text.DecimalFormat;
Import java.text.NumberFormat;

/***
*
* Amount
*
* If you need to calculate accurately, you must use string to build bigdecimal!!!
*
* Business calculations within Java cannot be used with float and double because they cannot be calculated accurately.
* But Java designers have provided programmers with a useful class bigdecimal,
* He can improve the lack of accurate calculations for the float and double classes.
* The BigDecimal class is located under the Java.maths class Package.
* It has many constructors, most commonly used:
* BigDecimal (double Val)
* BigDecimal (String Str)
* BigDecimal (BigInteger Val)
* BigDecimal (BigInteger unscaledval, int Scale)

*
* @author wxw
*
*/
public class Amountutil {

/***
* Reserved 2 decimal Places
* Rounding
* @param a
*
* @return
* Returns a double of type 2 decimal place
*/
public static Double get2double (double doubleval,int scale) {
if (null = = Doubleval) {
doubleval = new Double (0);
}
return new BigDecimal (doubleval). setscale (scale, bigdecimal.round_half_up). doublevalue ();
}

/***
* Formats a double type and retains the scale decimal number
* Rounding
* @param doubleval
* @param scale
* Scale must be a positive integer greater than 0, cannot be equal to 0
* @return
*/
public static String Formatby2scale (Double doubleval,int scale) {
if (null = = Doubleval) {
Doubleval = new Double (0);
}
StringBuffer sbstr = new StringBuffer ("0.");
for (int i = 0, i < scale; i++) {
sbstr.append ("0");
}
DecimalFormat myformat = new DecimalFormat (sbstr.tostring ());
Return Myformat.format (doubleval);
}

/***
* Double type add <font color= "red" >+</font><br/>
* round_half_up <font color= "red" > Rounding </font><br/>
* @param val1
*
* @param val2
*
* @param scale
* <font color= "red" > Reserve Scale decimal places </font><br/>
* @return
*/
public static double add (double val1,double val2,int Scale) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2) {
Val2 = new Double (0);
}
return new BigDecimal (double.tostring (val1)). add (new BigDecimal (double.tostring)). val2 (scale, bigdecimal.round_half_up). Doublevalue ();
}

/***
* Double type subtraction <font color= "red" >-</font><br/>
* round_half_up <font color= "red" > Rounding </font><br/>
* @param val1
*
* @param val2
*
* @param scale
* <font color= "red" > Reserve Scale decimal places </font><br/>
* @return
*/
public static double subtract (double val1,double val2,int Scale) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2) {
Val2 = new Double (0);
}
return new BigDecimal (double.tostring (val1)). subtract (new BigDecimal (double.tostring)). val2 (scale, bigdecimal.round_half_up). Doublevalue ();
}

/***
* Double type multiplication <font color= "red" >*</font><br/>
* round_half_up <font color= "red" > Rounding </font><br/>
* @param val1
*
* @param val2
*
* @param scale
* <font color= "red" > Reserve Scale decimal places </font><br/>
* @return
*/
public static double multiply (double val1,double val2,int Scale) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2) {
Val2 = new Double (0);
}
return new BigDecimal (double.tostring (val1)). multiply (new BigDecimal (double.tostring)). val2 (scale, bigdecimal.round_half_up). Doublevalue ();
}

/***
* Divide double type <font color= "red" >/</font><br/>
* round_half_up <font color= "red" > Rounding </font><br/>
* @param val1
*
* @param val2
*
* @param scale
* <font color= "red" > Reserve Scale decimal places </font><br/>
* @return
*/
public static double divide (double val1,double val2,int Scale) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2 | | val2 = = 0) {
Val2 = new Double (1);
}
return new BigDecimal (double.tostring (val1)). divide (new BigDecimal (double.tostring)). val2 (scale, bigdecimal.round_half_up). Doublevalue ();
}


/***
* Double type take-up <font color= "red" >%</font><br/>
* round_half_up <font color= "red" > Rounding </font><br/>
* @param val1
*
* @param val2
*
* @param scale
* <font color= "red" > Reserve Scale decimal places </font><br/>
* @return
*/
public static int Divideandremainder (Double val1,double val2,int Scale) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2 | | val2 = = 0) {
Val2 = new Double (1);
}
return new BigDecimal (double.tostring (val1)). divideandremainder (new BigDecimal (double.tostring (val2))) [1]. Setscale (scale, bigdecimal.round_half_up). intvalue ();
}

/***
* Format double type of data
*
* @param val
* @param FMT
* NumberFormat currency = numberformat.getcurrencyinstance (); Create a currency formatted reference
* NumberFormat percent = numberformat.getpercentinstance (); Establish a percentage formatted reference
* @param maximumfractiondigits
* If it is a percent setting decimal place (rounded)
* @return
*/
public static String Formatbynumberformat (Double Val,numberformat fmt,int maximumfractiondigits) {
If (fmt.equals (numberformat.getpercentinstance ())) {
Fmt.setmaximumfractiondigits (maximumfractiondigits); Percent decimal point up to 3 digits
}
return Fmt.format (val);

}

/***
* Compare size
*-1, 0, 1, that is, the left is larger than the right, return 1, equal return 0, smaller than the right return-1.
* @param doubleval
* @return
*/
public static int CompareTo (Double val1,double val2) {
if (null = = Val1) {
Val1 = new Double (0);
}
if (null = = Val2) {
Val2 = new Double (0);
}
return new BigDecimal (val1). compareTo (new BigDecimal (val2));
}


public static void main (string[] Args) {

System.out.println (amountutil.get2double (null,3));
System.out.println (amountutil.add (12.2155, null,4));
System.out.println (amountutil.subtract (12.2155, 1d,2));
System.out.println (amountutil.multiply (12.2155, 2d,2));
System.out.println (amountutil.divide (44.13, 2d,2));
System.out.println (amountutil.divideandremainder (43D, 8d,0));
System.out.println (amountutil.formatbynumberformat (0.123456, numberformat.getpercentinstance (), 3));
System.out.println (amountutil.formatby2scale (12.23457,3));


DecimalFormat df = new DecimalFormat ("0.00\u2030"); "\u2030" means multiply by 1000 and display as thousand fractions
System.out.println (df.format (12.1233)); 8-->1234567.89‰

DF = new DecimalFormat ("0,000.0#");//add A comma to the number
System.out.println (df.format (123456789.12345)); 5-->-1,234.57


DF = new DecimalFormat ("0");//do not keep decimal rounding
System.out.println (df.format (123456789.9876)); 5-->-1,234.57
}
}

Calculate the exact amount in Java BigDecimal

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.