Skip to content

Various Kotlin extensions for java.math.BigInteger and java.math.BigDecimal

Notifications You must be signed in to change notification settings

peterphmikkelsen/kotlin-bignumber-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Extension functions for BigInteger and BigDecimal

Various Kotlin extensions for java.math.BigInteger and java.math.BigDecimal

Adds convenient way to convert things into BigInteger and BigDecimal, this includes extension properties for Int, Long and Double. It also allows standard arithmatic between "BigNumber" and Int, Long and Double.

val bigIntArith = ((10.bi + 12) * 2 / 10) - 1 // = 3 (BigInteger)
val bigDecArith = ((10.bd + 12) * 2 / 10) - 1 // = 3.4 (BigDecimal)

Important: Note that RoundingMode.FLOOR is used when doing division with BigInteger, which replicates how Kotlin handles standard integer division. It is also important to note, that BigDecimal division uses a precision of 20 by default and uses RoundingMode.HALF_UP.

More Examples

var crazy = ((10L.bi + 12) * 2.0 / "10".bi) - 1 // = 3.4 (BigDecimal)
crazy += 2 // = 5.4 (BigDecimal)
floor(crazy) // = 5 (BigInteger)
ceil(crazy) // = 6 (BigInteger)

Important: Note that any arithmatic involving a Double will return a BigDecimal.

About

Various Kotlin extensions for java.math.BigInteger and java.math.BigDecimal

Topics

Resources

Stars

Watchers

Forks

Languages