Java big data class

Source: Internet
Author: User
Import Java. math. bigdecimal; import Java. math. biginteger; public class biginter {// default division operation precision, that is, the number of decimal places to be retained Private Static final int default_div_scale = 10; // This class cannot be instantiated private biginter () {}/*** provides precise addition operations. * @ Param V1 add count * @ Param V2 add count * @ return 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 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, the number of digits after the decimal point is accurate to *, and the number after the decimal point is rounded down. * @ Param V1 divisor * @ Param V2 divisor * @ return two parameter vendors */public static double Div (double V1, double V2) {return Div (V1, V2, default_div_scale);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines * the precision, and the number 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) {system. err. println ("division accuracy must be greater than 0! "); Return 0;} bigdecimal b1 = new bigdecimal (double. tostring (V1); bigdecimal b2 = new bigdecimal (double. tostring (V2); Return (b1.divide (B2, scale, bigdecimal. round_half_up )). doublevalue ();}/*** calculate the factorial! * @ Param n any int greater than or equal to 0 * @ return n! Value */public static biginteger getfactorial (int n) {If (n <0) {system. Err. println ("n must be greater than or equal to 0! "); Return New biginteger ("-1 ");} else if (n = 0) {return New biginteger (" 0 ");} // Replace the array with a string and construct biginteger result = new biginteger ("1"); For (; n> 0; n --) {// convert the number N into a string, and then construct a biginteger object to multiply result = result with the existing result. multiply (New biginteger (New INTEGER (n ). tostring ();} return result;} public static void main (string [] ARGs) {// What will be seen if we compile and run the following program? System. out. println (0.05 + 0.01); system. out. println (1.0-0.42); system. out. println (4.015*100); system. out. println (123.3/100); // 0.060000000000000005 // 0.5800000000000001 // 401.49999999999994 // 1.2329999999999999 // calculate the factorial. You can set N to a greater int n = 30; system. out. println ("Calculate the factorial of N" + N + "! = "+ Biginter. getfactorial (n); // use double to construct bigdecimal bd1 = new bigdecimal (0.1); system. out. println ("(bd1 = new bigdecimal (0.1) =" + bd1.tostring (); // use string to construct bigdecimal bd2 = new bigdecimal ("0.1"); system. out. println ("(bd2 = new bigdecimal (\" 0.1 \ ") =" + bd2.tostring (); bigdecimal bd3 = new bigdecimal ("0.10 "); // The equals method compares whether two bigdecimal objects are equal, returns true if they are equal, and returns false system if not. O Ut. println ("bd2.equals (bd3) =" + bd2.equals (bd3); // false // The compareto method compares the sizes of two bigdecimal objects. Equal returns 0, less than-1, if the value is greater than 1. System. out. println ("bd2.compareto (bd3) =" + bd2.compareto (bd3); // 0 // accurately calculates the system. out. println ("0.05 + 0.01 =" + biginter. add (0.05, 0.01); system. out. println ("1.0-0.42 =" + biginter. add (1.0, 0.42); system. out. println ("4.015*100 =" + biginter. add (4.015, 100); system. out. println ("123.3/100 =" + biginter. add (123.3, 100 ));}}

It is much easier to write than C ++...

Result:

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.