Simple usage of bigdecimal classes in Java _java

Source: Internet
Author: User

The example of this article tells the Java BigDecimal class simple usage, is the Java program design very practical skill, shares for everybody to consult. The specific usage analysis is as follows:

In general, when it comes to commercial computing in Java, we all know that we can't use float and double because they can't do accurate computations. But the Java designer provides a useful class bigdecimal for programmers, who can refine the flaws in the float and double classes that do not accurately compute. The BigDecimal class is located under the Java.maths class package. First, let's look at how to construct a BigDecimal object. It has a lot of constructors, and here's a selection of the two most commonly used to demonstrate: one is BigDecimal (double val) and the other is BigDecimal (String str). These two don't look very different, but as the API description says:

/*the results of this constructor can be somewhat unpredictable. One might assume 
that new BigDecimal (. 1) are exactly equal to. 1, but it are actually equal to 
. 10000000000000000555 11151231257827021181583404541015625. This are so because. 1 
cannot to represented exactly as a double (or, for that mat ter, as a binary fraction of any 
finite length). Thus, the Long value, being passed in to the constructor isn't 
exactly equal to. 1, appearances Nonwithstandin G.
The (String) constructor, on the other hand, are perfectly predictable:new BigDecimal
(". 1") is exactly equal to. 1, a s one would expect. Therefore, it is generally 
recommended that the (String) constructor being used in preference to this one.*/

That is, using double as the constructor of a parameter, it is not possible to construct an BigDecimal object precisely, and you need to specify a context, that is, to specify the exact bit. A BigDecimal object can be constructed precisely by using a string object as a constructor passed in as a parameter. Take a look at the following code:

Import java.math.*;
public class Testbigdecimal {public
  static void Main (String args[]) {
    BigDecimal bd = new BigDecimal ("10.123"); 
   bigdecimal bd1 = new BigDecimal (10.123);
    SYSTEM.OUT.PRINTLN (BD + "n" + bd1);
  }


Run after output:

10.123
10.1229999999999993320898283855058252811431884765625

So when we select a constructor, we need to look at the specific requirements.

In addition, many people will ask how to convert basic types, such as Int,float,double,long, and BigDecimal objects to each other. Very simple:

The base type is converted to the corresponding BigDecimal object through the constructor, while the BigDecimal class provides such as Intvalue (), Floatvalue (), Doublevalue (), Longvalue () method to convert the BigDecimal object to the corresponding value.

about how BigDecimal is calculated, I take a forum in a person's question post for example, to simply write the BigDecimal method of operation. The title is: Li Bai walk in the street, bring pot to buy wine. Encounter Shop plus one times, see flowers drink a bucket, five flowers and shops, drink light pot of wine, how many dou Li Bai pot of wine?

This problem should be pushed forward from behind, and to inverse, finally get the original volume of wine.

Import java.math.*;
public class Libai {public
  static void Main (String args[]) {
    BigDecimal volumn = new BigDecimal ("0");
    for (int i=0; i<5; i++) {      
      volumn = Volumn.add (New BigDecimal ("1"));
      Volumn = Volumn.divide (New BigDecimal ("2"));
    }
    System.out.print (volumn);
  }


Run Result:

0.96875

I hope this article will help you with your study of Java programming.

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.