Java float addition and subtraction Accuracy

Source: Internet
Author: User
Tags arithmetic operators
Java float addition and subtraction accuracy can be converted to bigdecimal when this field is obtained, while bigdecimal can be set. Float M = 12.22f; float c = 1.22f; bigdecimal b1 = new bigdecimal (float. tostring (m); bigdecimal b2 = new bigdecimal (float. tostring (c); system. out. println (m); system. out. println (c); float add = b1.add (B2 ). floatvalue (); system. out. println ("add =" + Add); float less = b1.subtract (B2 ). floatvalue (); system. out. println ("less =" + less); float multiply = b1.multiply (B2 ). floatvalue (); system. out. print Ln ("Multiply ========" + multiply); // set the exact decimal point for divide to solve the problem of Float divide = b1.divide (B2, 4, bigdecimal. round_half_even ). floatvalue (); system. out. println ("Divide ========" + divide ); ========================================================== === bigdecimal does not divide an exception in Java. lang. arithmeticexception: the data type of the non-terminating decimal expansion amount is bigdecimal. If division is not performed using the Divide Method of bigdecimal, an exception is thrown when there is an infinite number of loops. The exception is as follows: java. lang. arithmeticexc Eption: Non-terminating decimal expansion; no exact representable decimal result. at java. math. bigdecimal. divide (unknown source) Application Scenario: the unit price of a batch of medium customers is 1000 yuan/year, if calculated by month 1000/12 = 83. 3333333333 .... solution: Set the precise decimal point divide (XXXXX, 2, bigdecimal for divide. round_half_even) in Java, rounding is implemented through bigdecimal. Be sure to: bigdecimal is immutable. That is, like a string, a new bigdecimal is returned for the previous modification, such as setscale () and add. the rounding mode is bigdecimal. round_half_up bigdecimal defines the round-robin mode. The round-robin mode is used only when division or rounding is performed. For more information, see the j2se API documentation static intround_ceilingrounding mode to Round towards positive infinity. round static intround_downrounding mode to Round towards zero to positive infinity. round static intround_floorrounding mode to Round towards negative infinity to zero. round static intround_half_d to negative infinity Ownrounding mode to Round towards "Nearest Neighbor" unless both neighbors are equidistant, in which case round down. round to the nearest side, unless the two sides (distance) are equal. If so, round down, for example, 1.55 retains a decimal number and returns 1.5 static intround_half_evenrounding mode to Round towards the "Nearest Neighbor" unless both neighbors are equidistant, in which case, Round towards the even neigh. round to the nearest side (distance), unless the two sides (distance) are equal. If so, if the reserved digits are odd, use round_half_up. If it is an even number Use round_half_downstatic intround_half_uprounding mode to Round towards "Nearest Neighbor" unless both neighbors are equidistant, in which case round up. round to the nearest side (distance), unless the two sides (distance) are equal, if so, round up, 1.55 retain a decimal digit and the result is 1.6 static intround_unnecessaryrounding mode to assert that the requested operation has an exact result, hence norounding is necessary. the calculation result is accurate, and the rounding mode static intround_uprounding mode to round away fro is not required. M zero. round to the direction away from 0 ======================================== ========== how to apply the double variable of the bigdecimal type in Java to handle 16-bit valid numbers. In practical applications, You need to calculate and process larger or smaller numbers. The API class bigdecimal provided by Java in the Java. Math package is used to accurately calculate the number of valid 16 bits. Table 5.7 lists the main constructors and methods of the bigdecimal class. Table 5.7 bigdecimal class's main constructor and method constructor describes bigdecimal (INT) to create an object with the integer specified by the parameter. Bigdecimal (double) creates an object with the double precision value specified by the parameter. Bigdecimal (long) creates an object with the Length Integer specified by the parameter. Bigdecimal (string) creates an object with the value specified by the parameter in the string. The add (bigdecimal) bigdecimal object is described in the method of adding a table, and then this object is returned. Subtract (bigdecimal) values in the bigdecimal object subtract, and then return this object. Multiply the values in the multiply (bigdecimal) bigdecimal object and return this object. Divide (bigdecimal) values in the bigdecimal object are divided, and then this object is returned. Tostring () converts the value of the bigdecimal object to a string. Doublevalue () returns the values in the bigdecimal object with double precision. Floatvalue () returns the values in the bigdecimal object with a single precision. Longvalue () returns the value in the bigdecimal object as a long integer. Intvalue () returns the value in the bigdecimal object as an integer. Note: Generally, because the numeric type, such as double, cannot accurately represent a number greater than the 16-bit valid number, it makes sense to use the bigdecimal (string) constructor to create an object. In addition, bigdecimal creates an object. We cannot directly perform mathematical operations on the object using traditional arithmetic operators such as +,-, *, And, instead, the corresponding method must be called. The parameters in the method must also be bigdecimal objects. The constructor is a special method of a class that is used to create objects, especially objects with parameters. The constructor concept and writing technology will be detailed in chapter 6th of this book.
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.