Java.math.BigDecimal

Source: Internet
Author: User

Tag: equals str calculation requires print Mat IMA 1.0 display

When Java runs 1.0-0.8, the result is not 0.2, but 0.1999999999999996. This is because
Java adheres to the IEEE754 floating-point Arithmetic (floating-pointarithmetic) specification, using fractions and
Index to represent floating-point numbers. For example, 0.5 uses 1/2 to represent, and 0.75 uses 1/2+1/4 to represent,
0.875 will be represented using 1/2+1/4+1/8, while 0.1 will use
1/16+1/32+1/256+1/512+1/4096+1/8192+ ... The infinite loop goes on, cannot express accurately,
Thus, the error in the computation is caused.

Again for example the following program fragment

double a=0.1; double b=0.1; double c=0.1; if ((a+b+c) ==0.3) {System.out.println ("equals 0.3");} Else {System.out.println ("Not equal to 0.3");}

Because of the floating-point error, the result is "not equal to 0.3".
So use the = = with caution when using floating-point numbers.
But what about using = = and floating-point numbers at the same time?

The Java.math.BigDecimal class is required at this point. As an example of 1.0-0.8 just now, if you want to
The result of 0.2 can be calculated directly from new BigDecimal class.
Direct use of the program to demonstrate:

 Package Addfloatproject; Import Java.math.BigDecimal;  Public class Addfloat{bigdecimal anew BigDecimal ("1.0"); BigDecimal bnew BigDecimal ("0.8"); BigDecimal c= a.substract (b); System.out.println (c);}

At this point the result is 0.2

One way to create BigDecimal is to use a string, bigdecimal in creating is to parse the incoming character
String to the default precision for the next operation. BigDecimal is available with plus (), Substract (
), multiply (), divide (), Equals (), CompareTo (), and other methods.

Example Two

 import   Java.math.BigDecimal;  public  class   addfloat{bigdecimal a  = new  BigDecimal (" 0.1 "); BigDecimal b  = new  BigDecimal ("0.1"  = new  BigDecimal ("0.1"  = new  BigDecimal ("0.3"  if   (A.add (b). Add (c). Equals (Result) {System.out.println ( equals 0.3 ");}  else  {system.out.println ( "Not equal to 0.3"  

The display result is equal to 0.3;

Java.math.BigDecimal

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.