ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/JAVA

[JAVA] BigInteger ํด๋ž˜์Šค

NaNaRin๐Ÿ™ƒ 2021. 1. 17. 16:36

docs.oracle.com/en/java/javase/15/docs/api/java.base/java/math/BigInteger.html

 

BigInteger (Java SE 15 & JDK 15)

All Implemented Interfaces: Serializable, Comparable public class BigInteger extends Number implements Comparable Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's pri

docs.oracle.com

java.math์— ์†ํ•ด์žˆ๋Š” ํด๋ž˜์Šค.

 

long ํƒ€์ž…์˜ ๋ฒ”์œ„๋ณด๋‹ค ๋” ํฐ ์ˆ˜๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.


BigInteger :

- ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์–ด ์ˆซ์ž์˜ ๋ฒ”์œ„์— ์ œํ•œ์ด ์—†๋‹ค.

 

  • BigInteger(String str) : str ๊ฐ์ฒด ์ƒ์„ฑ

 

  • BigInteger add(BigInteger val) : ๋ง์…ˆ +
  • BigInteger subtract(BigInteger val) : ๋บ„์…ˆ -
  • BigInteger multiply(BigInteger val) : ๊ณฑ์…ˆ *
  • BigInteger divide(BigInteger val) : ๋‚˜๋ˆ—์…ˆ /
  • BigInteger remainder(BigInteger val) : ๋‚˜๋จธ์ง€ %
  • int intValue() : intํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜
  • long longValue() : longํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜
  • float floatValue() : floatํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜
  • double doubleValue() : doubleํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜
  • String toString() : Stringํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜

 

BigInteger.add(val)

BigInteger.subtract(val)

BigInteger.multiply(val)

BigInteger.divide(val)

BigInteger.remainder(val)

BigInteger.intValue()

BigInteger.longValue()

BigInteger.floatValue()

BigInteger.doubleValue()

BigInteger.toString()

 


์˜ˆ์ œ

java
๋‹ซ๊ธฐ
import java.math.BigInteger; public class BigIntegerEx { โ€Œpublic static void main(String[] args) { โ€Œโ€ŒBigInteger bi1 = new BigInteger("123456789"); โ€Œโ€ŒBigInteger bi2 = new BigInteger("987654321"); โ€Œโ€Œ โ€Œโ€ŒSystem.out.println(bi1.add(bi2)); โ€Œโ€ŒSystem.out.println(bi1.subtract(bi2)); โ€Œโ€ŒSystem.out.println(bi1.multiply(bi2)); โ€Œโ€ŒSystem.out.println(bi1.divide(bi2)); โ€Œโ€ŒSystem.out.println(bi1.remainder(bi2)); โ€Œโ€ŒSystem.out.println(); โ€Œโ€Œ โ€Œโ€ŒSystem.out.println(bi1.intValue()); โ€Œโ€ŒSystem.out.println(bi1.longValue()); โ€Œโ€ŒSystem.out.println(bi1.floatValue()); โ€Œโ€ŒSystem.out.println(bi1.doubleValue()); โ€Œโ€ŒSystem.out.println(bi1.toString()); โ€Œ} }

 

์ถœ๋ ฅ

1111111110
-864197532
121932631112635269
0
123456789

123456789
123456789
1.23456792E8
1.23456789E8
123456789

 

 

์‚ฌ์šฉ ์˜ˆ : nanarin.tistory.com/71