Learning from BigDecimal in Java

Source: Internet
Author: User

Do Java work, but look at some of the bottom of things (or I did not see), a bit of neglect of Java learning.

A class that has been used recently has been bigdecimal, but it is ambiguous to use it there, without delving into the details of the class, and feeling that it can no longer be dragged.

BigDecimal, from the name of a large number of operations, not only this, but also widely used for the precise operation of decimals.

This class is useful when you come into contact with money-related calculations.

First look at an example

1  PackageCom.tuhooo.bigdecimal;2 3 /**4 * Created by tuhooo on 2017/8/21.5  */6  Public classDemo1 {7      Public Static voidMain (string[] args) {8System.out.println (0.06 + 0.01);9System.out.println (1.002-0.4);TenSYSTEM.OUT.PRINTLN (4.015 * 100); OneSystem.out.println (333.1/500); A     } -}

Reasoning, the above operation has not yet involved the infinite loop decimal, but the computer gives the result is such a drop:

In fact, the reason is very simple, the accuracy of the computer's representation number is limited, the computer at the bottom of the binary to represent the data, especially when the decimal point is more accurate.

Think about that, it is not acceptable to say that up to the point is financial-related, if this operation is in a place where money is concerned.

Java provides a good class--bigdecimal to solve this problem.

1  PackageCom.tuhooo.bigdecimal;2 3 ImportJava.math.BigDecimal;4 5 /**6 * Created by tuhooo on 2017/8/21.7  */8  Public classDemo2 {9      Public Static voidMain (string[] args) {TenSystem.out.println (NewBigDecimal ("0.06"). Add (NewBigDecimal ("0.01"))); OneSystem.out.println (NewBigDecimal ("1.002"). Subtract (NewBigDecimal ("0.4"))); ASystem.out.println (NewBigDecimal ("4.015"). Multiply (NewBigDecimal ("100"))); -System.out.println (NewBigDecimal ("333.1"). Divide (NewBigDecimal ("500"))); -     } the}

Look at the following effect:

Let's look at one more example:

1  PackageCom.tuhooo.bigdecimal;2 3 ImportJava.math.BigDecimal;4 5 /**6 * Created by tuhooo on 2017/8/21.7  */8  Public classDemo3 {9      Public Static voidMain (string[] args) {TenSystem.out.println (NewBigDecimal (0.06). Add (NewBigDecimal (0.01))); OneSystem.out.println (NewBigDecimal (1.002). Subtract (NewBigDecimal (0.4))); ASystem.out.println (NewBigDecimal (4.015). Multiply (NewBigDecimal (100))); -System.out.println (NewBigDecimal (333.1). Divide (NewBigDecimal (500))); -     } the}

It can be found here, it is not used bigdecimal will have effect, and bigdecimal of the structure of the way also has a relationship

To be Continued ...

Learning from BigDecimal in Java

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.