Java Numerical Precision output control (contains BigDecimal) __java

Source: Internet
Author: User

Import java.io.*; Import java.text.*; Import java.math.*; Import java.util.*; gaojingdutest {public static void main (string[] args) {//double decimal-point control output double a=1113.141592653; DecimalFormat formatter1=new DecimalFormat ("#.000000"); System.out.println (Formatter1.format (a)); The decimal point control output String b= "1113.141592653" for BigDecimal type data; Method 1 BigDecimal bd1=new BigDecimal (b); DecimalFormat formatter2=new DecimalFormat ("#.000000"); System.out.println (Formatter2.format (Bd1.doublevalue ())); Method 2, the first parameter of the Setscale method in BigDecimal//is the number of precision digits we want to specify. Here I set it to 6 decimal places, but remember, if//rounded after the result of a few decimal digits is 0, it will be omitted, that is, 0 is not output, see the following test BigDecimal bd2=new BigDecimal (b); System.out.println (Bd2.setscale (6,bigdecimal.round_half_up). Doublevalue ()); Test System.out.println (); System.out.println ("The following is test:"); BigDecimal b0=new BigDecimal ("1.00000000"); System.out.println (B0.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b1=new BigDecimal ("1.00000010"); System.out.println (B1.setscale (6,bigdecimal.round_half_up). DoublEvalue ()); BigDecimal b2=new BigDecimal ("1.00000020"); System.out.println (B2.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b3=new BigDecimal ("1.00000030"); System.out.println (B3.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b4=new BigDecimal ("1.00000040"); System.out.println (B4.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b5=new BigDecimal ("1.00000050"); System.out.println (B5.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b6=new BigDecimal ("1.00000060"); System.out.println (B6.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b7=new BigDecimal ("1.00000070"); System.out.println (B7.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b8=new BigDecimal ("1.00000080"); System.out.println (B8.setscale (6,bigdecimal.round_half_up). Doublevalue ()); BigDecimal b9=new BigDecimal ("1.00000090"); System.out.println (B9.setscale (6,bigdecimal.round_half_up). Doublevalue ()); /* Output results are as follows: 1113.141593 1113.141593 1113.141593 below for Test: 1.0 1.0 1.0 1.0 1.0 1.000001, 1.000001 1.000001 1.000001 *}}  

--> the problem of high accuracy is indeed a big problem, but unfortunately this year.

PS: After writing, we found that Java also has such a way to control the output accuracy

System.out.printf ("%.6f/n", 0.1);

The output is: 0.100000

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.