Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides precise floating-point arithmetic, including subtraction and rounding.

Source: Internet
Author: User

Package com.minxinloan.utils;

Import Java.math.BigDecimal;

public class Arith {
Source file Arith.java:

/**
* Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides accurate floating-point arithmetic, including subtraction and rounding.
*/

Default division Operation Precision
private static final int def_div_scale = 10;

This class cannot be instantiated
Private Arith () {
}

/**
* provides accurate addition operations.
*
* @param v1
* Summand
* @param v2
* Addend
* @return of two parameters 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 accurate subtraction operations.
*
* @param v1
* Minuend
* @param v2
* meiosis
* @return The difference of 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 accurate multiplication operations.
*
* @param v1
* by multiplier
* @param v2
* Multiplier
* @return The 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 ();
}

/**
* Provide (relative) accurate division operations, when there are no more than the case, accurate to 10 digits after the decimal point, after the number rounded.
*
* @param v1
* Dividend
* @param v2
* Divisor
* @return two parameters of the quotient
*/

public static double div (double v1, double v2) {
Return Div (v1, v2, Def_div_scale);
}

/**
* Provide (relative) accurate division operations. When an exception occurs, the precision is specified by the scale parameter, and the subsequent number is rounded.
*
* @param v1
* Dividend
* @param v2
* Divisor
* @param scale
* Indicates the need to be accurate to the decimal point after several.
* @return two parameters of the quotient
*/

public static double div (double v1, double v2, int scale) {
if (Scale < 0) {
throw new IllegalArgumentException ("The scale must is 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 rounding of decimal digits.
*
* @param v
* Numbers that need to be rounded
* @param scale
* Keep several after the decimal point
* Results after rounding @return
*/

public static double round (double V, int. scale) {
if (Scale < 0) {
throw new IllegalArgumentException ("The scale must is 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). Doublevalue ();
}
};

/**
* Formatted as two decimal places
*
* @param val value to be formatted
* @return Two-bit decimal value after formatting
*/
public static String Formattwodecimal (double val) {
Determine the Val decimal place, if only 3 decimal places this value plus 0.0001, processing such as 7.245 after the format of the value of 7.24 accuracy problem
Double val2 = val;
String strval = string.valueof (val2);
if (Strval.indexof (".") > 0) {
String Dec = strval.substring (Strval.indexof (".") + 1);
if (dec.length () = = 3) {
Val2 + = 0.0001;
}
}
DecimalFormat df = new DecimalFormat ("#0.00");
String formated =df.format (VAL2);
return formated;
}

Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides precise floating-point arithmetic, including subtraction and rounding.

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.