Encapsulation and application of Bigdicemal class in JAVA __java

Source: Internet
Author: User
Tags mul

Encapsulation application of Bigdicemal class in Java

Unfortunately used before, and now used, so the study of the following is as follows:

Scale the decimal point to retain several.

Package jp.co.**.**.com.utl;

Import Java.math.BigDecimal;

public class Utils {

/** Precision * *
private static final int def_div_scale = 10;

/**
* Add, BigDecimal
*
* @param v1
* @param v2
* @return Result
*/
public static BigDecimal Add (Object V1, Object v2) {
BigDecimal result = null;

BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
result = B1.add (B2);
return result;
}

/**
* Reduced, BigDecimal use
*
* @param v1
* @param v2
* @return Result
*/
public static BigDecimal Sub (object V1, Object v2) {
BigDecimal result = null;
if (v1!=null && v2!=null) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
result = B1.subtract (B2);
// }
return result;
}

/**
* Multiply, BigDecimal use
*
* @param v1
* @param v2
* @return Result
*/
public static BigDecimal Mul (Object V1, Object v2) {
BigDecimal result = null;
if (v1!=null && v2!=null) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
result = B1.multiply (B2);
// }
return result;
}

/**
* Except, BigDecimal used
*
* @param v1
* @param v2
* @return Result
*/
public static BigDecimal Div (Object V1, Object v2) {
BigDecimal result = null;
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
result = B1.divide (B2);
return result;
}

/**
*
* @param v1
* @param v2
* @param scale
* @return Result
*/
public static BigDecimal Div (Object V1, object v2, int scale) {
BigDecimal result = null;
if (Scale < 0) {
throw New IllegalArgumentException (
"The scale must be a positive integer or zero");
}
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
result = B1.divide (b2, scale, bigdecimal.round_half_up);
return result;
}

public static BigDecimal 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);
}

/**
* @param v Summand
* @param v2 Addends
* @return two parameters of the 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 ();
}

}
There are a lot of methods and attributes, you can check the Java API on your own. Very simple.

Before rounding, the scale of the exact intermediate result of the logic is the preferred scale for the operation. If the precision number cannot be used to represent the exact numerical result, the group membership selects a set of numbers to return and reduces the scale of the result from the scale of the intermediate result to the minimum scale that can represent the actual returned precision digits. If the exact result can be expressed with a maximum of precision digits, the result representation of the scale with the closest preferred scale is returned. In particular, by removing the end 0 and decreasing the scale, you can use less than precision numbers to represent the exact quotient of an expression. For example, use the floor rounding mode to round the result to three digits,
19/100 = 0.19//integer=19, scale=2
But
21/110 = 0.190//integer=190, scale=3

Note that for addition, subtraction, and multiplication, the scale reduction will equal the number of digits of the exact result that was discarded. If rounding causes the carry propagation to create a new high, the additional number of the result is discarded when a new digit is not created.

BigDecimal
This principle is also mentioned in the book effective Java, where float and double can only be used for scientific calculations or engineering calculations, and we use java.math.BigDecimal in business calculations. BigDecimal There are 4 ways to make it, and we don't care about the two that we use BigInteger to build, then there are two, and they are:
BigDecimal (double val)
Translates a double into a BigDecimal.
BigDecimal (String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
The API outlined above is fairly clear, and usually the one above is easier to use. We may not even want to use it, what will be the problem. When there is a problem, it is found that there is a detailed description of the method in the above paragraph:
Note:the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal (. 1) are exactly equal to. 1, but it are actually equal to. 1000000000000000055511151231 257827021181583404541015625. This are so because. 1 cannot to represented exactly as a double (or, for that matter, as a bi nary fraction of any finite length). Thus, the Long value, being passed in to the constructor are not exactly equal to. 1, appearances nonwithstanding.
The (String) constructor, on the other hand, are perfectly predictable:new BigDecimal (". 1") is exactly equal to. 1, as one would expect. Therefore, it is generally recommended that the (String) constructor being used in preference to this one.

It turns out that if we need to calculate accurately, we have to use string to build BigDecimal. The example in "effective Java" is a string to make BigDecimal, but the book does not emphasize this, perhaps a small mistake.

Solution
Now we can solve the problem by using the BigDecimal and making sure to use string to make it.
But imagine, if we're going to do an addition, we need to convert two floating-point numbers to string, then enough to cause BigDecimal, call the Add method on one of them, pass in another as a parameter, and then turn the result of the operation (BigDecimal) into a floating-point number. Can you endure such a tedious process? Above we provide a tool class arith to simplify the operation. It provides the following static methods, including subtraction and rounding:
public static double Add (Double v1,double v2)
public static double sub (double v1,double v2)
public static double Mul (Double v1,double v2)
public static double div (Double v1,double v2)
public static double div (double v1,double v2,int scale)
public static double round (double v,int scale)

 

Related Article

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.