Big numbers BigInteger and BigDecimal in JAVA

Source: Internet
Author: User

BigInteger and BigDecimal are existing classes in the java. math package. The former represents an integer, and the latter represents a floating point.

Why use big numbers?

1) BigInteger: an integer of any precision is supported. It can accurately represent an integer of any size without any information loss During computation.

2) BigInteger: It can accurately represent decimal places of any precision without any information loss during the operation.


Note: you cannot directly use symbols such as + or-to use large numbers. For example:

Import java. math. bigInteger; public class Main {public static void main (String [] args) {int a = 231, B = 423, c = 1393; BigInteger x, y, z, ans; x = BigInteger. valueOf (a); y = BigInteger. valueOf (B); z = BigInteger. valueOf (c); ans = x. add (y); // add System. out. print (ans + ""); ans = z. divide (y); // division operation System. out. print (ans + ""); ans = x. mod (z); // modulo operation System. out. print (ans + ""); if (ans. compareTo (x) = 0) System. out. println ("1 ");}}

Calculation Result: 654 3 231 1


You can use the following methods:
BigInteger add (BigInteger other)
BigInteger subtract (BigInteger other)
BigInteger multiply (BigInteger other)
BigInteger divide (BigInteger other)
BigInteger mod (BigInteger other)
Int compareTo (BigInteger other)
Static BigInteger valueOf (long x)

This article is from the blog of "Italian suckling pig", please be sure to keep this source http://lovesisi.blog.51cto.com/6414433/1301207

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.