Java accurate calculation Method __ algorithm

Source: Internet
Author: User
Tags abs mul
/**  * because Java's simple type does not accurately operate on floating-point numbers, this tool class provides accurate floating-point operations, including subtraction and rounding.

 */package com.bwsk.common.utils;

Import Java.math.BigDecimal;
 /** * Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides precise floating-point operations, including subtraction and rounding.

	*/public class Arithutil {//default division precision private static final int def_div_scale = 10;
	 This class cannot instantiate private Arithutil () {}/** * provides exact addition operations. * * @param v1 * summand * @param v2 * addends * @return two parameters and/or public static double add (Dou
		BLE v1, double v2) {BigDecimal B1 = new BigDecimal (double.tostring (v1));
		BigDecimal b2 = new BigDecimal (double.tostring (v2));
	Return B1.add (B2). Doublevalue ();
	 /** * Provides an accurate subtraction operation. * * @param v1 * Bing * @param v2 * * @return two parameters of the difference * * public static double sub (dou
		BLE v1, double v2) {BigDecimal B1 = new BigDecimal (double.tostring (v1));
		BigDecimal b2 = new BigDecimal (double.tostring (v2));
	Return B1.subtract (B2). Doublevalue ();
	 /** * Provides an accurate multiplication operation. * * @parAM V1 * Multiplier * @param v2 * multiplier * @return two parameters of product/public static double Mul (double v1, dou
		BLE v2) {BigDecimal B1 = new BigDecimal (double.tostring));
		BigDecimal b2 = new BigDecimal (double.tostring (v2));
	Return b1.multiply (B2). Doublevalue ();
	 /** * provides (relative) accurate division operations, which, when in addition to the circumstances, are accurate to 10 digits after the decimal point, after which the numbers are rounded. * * @param v1 * Dividend * @param v2 * divisor * @return Two parameters of the merchant/public static double div (dou
	BLE v1, double v2) {return div (v1, v2, Def_div_scale); /** * provides (relative) precise division operations.
	 When an exception occurs, the precision is specified by the scale parameter, and subsequent digits are rounded.
	 * @param v1 * Dividend * @param v2 * @param scale * Indicates the need to be accurate to several decimal places. * @return Two-parameter merchant/public static double div (double v1, double v2, int scale) {if (Scale < 0) {throw new Ill
		Egalargumentexception ("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 accurate decimal rounding processing. * * @param v * need to rounded the number * @param scale * After the decimal point to retain a few * @return rounded results * * public Stati C Double round (double v, int scale) {if (Scale < 0) {throw new IllegalArgumentException ("The scale Mus
		t 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). Doublevalue (); /** * @param str1 * Operand 1 * @param str2 * Operands 2 * The result of multiplying the number represented by the two strings * * * public s Tatic double mulstrtodouble (String str1, String str2) {return arithutil. Mul (double.parsedouble), str1
	Double (STR2)); /** * @param str1 * Operand 1 * @param str2 * operation number 2 * @return Two string The result of the addition of the number is * * Public s Tatic Double AddStrtodouble (String str1, String str2) {return arithutil. Add (Double.parsedouble (STR1), double.parsedouble (STR2)); /** * @param str1 * Operand 1 * @param str2 * Operand 2 * @return The result of a number subtracted from two strings * * Public s Tatic double substrtodouble (String str1, String str2) {return arithutil. Sub (double.parsedouble (str1), Double.Parse
	Double (STR2)); 
	 /** * @param str1 * Operand 1 * @param str2 * Operands 2 * @param count * Keep several decimal rounded
		* Multiplies the number represented by two strings and preserves the result of the Count bit decimal * * public static double mulstrtodouble (string str1, String str2, int count) {
	Return Arithutil.round (Arithutil.mul (double.parsedouble (STR1), double.parsedouble (STR2)), count); /** * Method Description: Determine whether two double is equal input parameters: * * @param d1 * @param d2 * @return return Parameters: Boolean finish date: June 29, 2015 6:45:
		33 Version: v 1.0 */public static Boolean equaldouble (double D1, double D2) {BigDecimal bd1 = new BigDecimal (D1); BigDecimal Bd2 = new BIgdecimal (D2);
	Return Bd1.compareto (bd2) = = 0;  /** * Method Description: Determine whether two D1 is greater than D2 input parameter: * * @param d1 * @param d2 * @return return Parameters: Boolean finish date: June 29, 2015 6:45:33
		Version: v 1.0 */public static Boolean greatterdouble (double D1, double D2) {BigDecimal bd1 = new BigDecimal (D1);
		BigDecimal bd2 = new BigDecimal (D2);
	Return Bd1.compareto (BD2) > 0;  /** * Method Description: Determine whether two D1 is less than D2 input parameter: * * @param d1 * @param d2 * @return return Parameters: Boolean finish date: June 29, 2015 6:45:33
		Version: v 1.0 */public static Boolean lighterdouble (double D1, double D2) {BigDecimal bd1 = new BigDecimal (D1);
		BigDecimal bd2 = new BigDecimal (D2);
	Return Bd1.compareto (BD2) < 0;  /** * Addition Operation (EAST) * * @param A * @param b * @return/public static BigDecimal Add (BigDecimal A, BigDecimal
	b) {return a.add (b);
	/** * Take absolute value (east) * @param A * @return/public static BigDecimal abs (BigDecimal a) {return a.abs ();
 }
}

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.