Calculation and control method of floating-point precision for JAVA double or float __java

Source: Internet
Author: User

This article describes how to compute the precision of floating-point numbers in double or float in Java, providing a variety of parameters in Java to achieve the different control of precision. Specific examples are as follows:

* * Created on 2005-6-5 * Author Stephen * Email zhoujianqiang at gmail DOT com * CopyRight (C) 2005-2008, All rights Rese RVed. * * Package com.soft4j.utility; Import Java.math.BigDecimal; /** * Some commonly used tool methods related to decimal precision (rounding, etc.). * * Float/double precision values are divided into the following: <br> * Java.math.BigDecimal.ROUND_UP <br> * Java.math.BigDecimal.ROUND_DOWN <br> * Java.math.BigDecimal.ROUND_CEILING <br> * Java.math.BigDecimal.ROUND_FLOOR <br> * java.math.bigdecimal.round_half_up<br> * Java.math.BigDecimal.ROUND_HALF_DOWN <br> * Java.math.BigDecimal.ROUND_HALF_EVEN <br> * * @author Stephen * @version 1.0.0/Public final class Roundtool {/** * Accuracy of double data. * <p> * For example: <br> * Double value = 100.345678; <br> DOUBLE ret = round (VALUE,4,BIGDECIMAL.ROUND_HALF_UP); <br> * ret for 100.3457 <br> * * @param value * Double data. * @param scale * precision digits (number of decimal places to keep). * @param Roundingmode * Precision value method. * @return The data after the precision calculation. */public static double rounduble value, int scale, int roundingmode) {BigDecimal bd = new BigDecimal (value); BD = Bd.setscale (scale, roundingmode); d Ouble d = bd.doublevalue (); BD = NULL; return D; /** * The Main method used for testing. * * @param argc * Run parameters. */public static void Main (string[] argc) {//below to retain 2 decimal places as an example//round_up///As long as a decimal number greater than 0 exists after the 2nd digit, the 2nd digit is +1 System.out.println (ROUND (12.3401,2,BIGDECIMAL.ROUND_UP)); /12.35 System.out.println (Round ( -12.3401,2,bigdecimal.round_up));//-12.35//round_down//round_up opposite// Discard all decimal System.out.println (round (12.349,2,bigdecimal.round_down)) immediately following the 2nd digit,//12.34 System.out.println (- 12.349,2,bigdecimal.round_down))//-12.34//round_ceiling//If the number >0 is the same as the round_up effect if the number <0 is the same as the Round_down function System.out.println (Round (12.3401,2,bigdecimal.round_ceiling));//12.35 System.out.println (Round ( -12.349,2, bigdecimal.round_ceiling))//-12.34//round_floor//If the number >0 is the same as the Round_down effect if the number <0 is the same as the ROUND_UP function System.out.println (Round (12.349,2,bigdecimal.round_floor));//12.34 System.out.println (Round ( -12).3401,2,bigdecimal.round_floor));//-12.35//round_half_up [most commonly used for this method]//If the 3rd digit >=5, then the 2nd digit +1//NOTE: Look at the value of the 3rd digit only, System.out.println (Round (12.345,2,BIGDECIMAL.ROUND_HALF_UP)) that do not consider decimals after the 3rd digit,//12.35 System.out.println ( 12.3449,2,BIGDECIMAL.ROUND_HALF_UP));//12.34 System.out.println (ROUND ( -12.345,2,BIGDECIMAL.ROUND_HALF_UP));//- 12.35 System.out.println (Round ( -12.3449,2,bigdecimal.round_half_up))//-12.34//round_half_down//If the 3rd digit >=5, Do round_up///If the 3rd digit <5, then do Round_down System.out.println (round (12.345,2,bigdecimal.round_half_down));//12.35 System.out.println (Round (12.3449,2,bigdecimal.round_half_down));//12.34 System.out.println (Round ( -12.345,2, Bigdecimal.round_half_down));//-12.35 System.out.println (ROUND ( -12.3449,2,bigdecimal.round_half_down));//-12.34 Round_half_even//If the 3rd digit is an even number, do round_half_down//If the 3rd digit is odd, do round_half_up System.out.println (ROUND, Bigdecimal.round_half_even));//12.35 System.out.println (ROUND (12.345,2,bigdecimal.round_half_even));//12.35}}

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.