Java precision Floating Point calculation tool

Source: Internet
Author: User

Because Java's simple types cannot accurately perform floating-point operations, this tool class provides precise floating-point operations, including addition, subtraction, multiplication, division, and rounding. It can be used directly in the past, which is very convenient.

Import java. Math. bigdecimal;/*** Because Java's simple type cannot accurately perform operations on floating point numbers, * this tool class provides precise floating point operations, including addition, subtraction, multiplication, division, and rounding. */Public class Arith {// default Division calculation precision: Private Static final int def_div_scale = 10; // This class cannot be instantiated private Arith () {}/*** provides precise addition operations. ** @ Param V1 * Add Number * @ Param V2 * Add Number * @ return and */public static double add (double V1, double V2) {bigdecimal b1 = new bigdecimal (V1); bigdecimal b2 = new bigdecimal (V2); Return b1.add (B2 ). doublevalue ();}/*** provides precise subtraction. ** @ Param V1 * subtrahend * @ Param V2 * subtrahend * @ Return Difference Between 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 exact multiplication. ** @ Param V1 * multiplier * @ Param V2 * multiplier * @ return 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 ();}/*** provides (relatively) Precise Division operations. In case of division, * is accurate to 10 digits after the decimal point, and the digits after the decimal point are rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ return two parameters */public static double Div (double V1, double V2) {return Div (V1, v2, def_div_scale);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines the precision. * The number after division is rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ Param scale * indicates the number of digits after the decimal point. * @ Return operator of two parameters */public static double Div (double V1, double V2, int scale) {If (scale <0) {Throw new illegalargumentexception ("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 precise rounded decimal places. ** @ Param v * number to be rounded off * @ Param scale * number of digits to be retained after the decimal point * @ return result after rounding */public static double 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 ). doublevalue ();}};

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.