sungiant
sungiant

Reputation: 3222

How to create a BigInt by rounding from a Double in Scala

How can I convert a large double, one that won't fit into the Long data type:

var ex = 10e28 : Double;
ex -= 0.25

to a BigInt? Also making sure to round to the nearest BigInt.

Upvotes: 3

Views: 2231

Answers (1)

Travis Brown
Travis Brown

Reputation: 139038

I'm assuming that by "nearest" you mean you want e.g. 10.49 rounded to 10, 10.5 rounded to 11, etc. If so you can write the following:

BigDecimal(ex).setScale(0, BigDecimal.RoundingMode.HALF_UP).toBigInt

See my answer here for more information about the rounding modes.

Upvotes: 1

Related Questions