High-precision integers and high-precision decimals in Java

Source: Internet
Author: User
Tags pow

In the actual coding, you will encounter many high-precision cases, for example, in the calculation of money need to retain high-precision decimals, so that the calculation will not be too much error:

In the following code, we verify that when the number of two float types is added, the resulting result is error with our expected result, and in order to reduce and prevent this error, we need to use the BigInteger class and the BigDecimal class to calculate.

 PackageCom.ietree.base.number;ImportJava.math.BigDecimal;ImportJava.math.BigInteger; Public classBigintegertest { Public Static voidMain (string[] args) {floatF1 = 123.01f + 2.01f; //expected output: 125.02, actual output: 125.020004System.out.println (F1); //expected output: 125.02, actual output: 125.02000000000001System.out.println (123.01 + 2.01); System.out.println ("==============================="); //High-precision integer testingBigInteger bint1 =NewBigInteger ("125"); BigInteger Bint2=NewBigInteger ("999");        BigInteger tmp; //AddTMP =Bint1.add (Bint2); System.out.println ("Bint1 + Bint2 =" +tmp); //SubtractTMP =bint2.subtract (BINT1); System.out.println ("Bint2-bint1 =" +tmp); //MultiplyTMP =bint1.multiply (Bint2); System.out.println ("Bint1 * Bint2 =" +tmp); //DivideTMP =bint2.divide (BINT1); System.out.println ("Bint2/bint1 =" +tmp); //Find remainderTMP =Bint2.remainder (BINT1); System.out.println ("Bint2% Bint1 =" +tmp); //seeking the second partyTMP = BINT2.POW (2); System.out.println ("Bint2 two times =" +tmp); System.out.println ("======================================"); //High Precision Fractional testBigDecimal BD1 =NewBigDecimal (123.01); BigDecimal Bd2=NewBigDecimal (2.01);        BIGDECIMAL BD; //AddBD =Bd1.add (BD2); System.out.println ("BD1 + Bd2 =" +BD); //SubtractBD =bd1.subtract (BD2); System.out.println ("BD2-BD1 =" +BD); //MultiplyBD =bd1.multiply (BD2); System.out.println ("Bd1 * Bd2 =" +BD); //Divide//bd = Bd1.divide (BD2);BD = Bd1.divide (NewBigDecimal (2.0)); System.out.println ("bd1/2.0 =" +BD); //Find remainderBD =Bd1.remainder (BD2); System.out.println ("Bd2% BD1 =" +BD); //seeking the second partyBD = Bd1.pow (3); System.out.println ("Bd2 three times =" +BD); System.out.println ("======================================"); //rounding reserved decimal digitsBigDecimal Bd3 =NewBigDecimal (123.01). Setscale (5,5); System.out.println ("Bd3 =" +Bd3); }}

High-precision integers and high-precision decimals in Java

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.