Java small number processing, BigDecimal class application

Source: Internet
Author: User

1. Construct a BigDecimal object:

1         // Create a BigDecimal object, initialization must use a string, because numeric initialization results in approximate values, inaccurate 2         New BigDecimal ("1.232324"); 3 4         // to replace floating-point numbers with BigDecimal objects 5         double d = 1.3422424; 6         BigDecimal bigdecimaltwo =bigdecimal.valueof (d);
View Code

2. BigDecimal member Method:

1         //bigdecimalone plus bigdecimaltwo, then return a new object2 Bigdecimalone.add (bigdecimaltwo);3         4         //Bigdecimalone minus Bigdecimaltwo, then returns a new object5 bigdecimalone.subtract (bigdecimaltwo);6 7         //bigdecimalone times Bigdecimaltwo, then returns a new object8 bigdecimalone.multiply (bigdecimaltwo);9 Ten         //Bigdecimalone divided by Bigdecimaltwo and then returns a new object OneBigdecimalone.divide (Bigdecimaltwo);
View Code

3. BigDecimal objects into basic data types:

        // converts the value of the BigDecimal object to the corresponding string         bigdecimalone.tostring ();         // BigDecimal to return a double value         Bigdecimalone.doublevalue ();         // BigDecimal to return float         Bigdecimalone.floatvalue ();         // BigDecimal to return a Long value         Bigdecimalone.longvalue ();         // BigDecimal returns an int value        Bigdecimalone.intvalue ();
View Code

4, the way of rounding in Java:

Java decimal Processing, BigDecimal class application

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.