Skip to content
Advertisement

Tag: bigdecimal

Big Decimal array not sorting 0 and 000.000

I was solving a java HackerRank problem in which I had to sort an array consisting decimal numbers using BigDecimal class in descending order. The solution works fine except for one case in which 0 and 000.000 comes. Now as they are equal, the problem tells us not keep them in the same order of their occurrence, but it is

How to round a BigDecimal before the decimal?

I have a BigDecimal like this var a = new BigDecimal(“1234.56”) and I want to round it to commercially to two places before the decimal, so that I get something equivalent to var b = new BigDecimal(“1200.00”) so that (new BigDecimal(“1200.00”)).equals(b). My best approach currently is var b = new BigDecimal(a.setScale(-2, RoundingMode.HALF_UP).toPlainString()).setScale(2); but I’m unsure if this is the right

Create BigDecimal from unscaled long

I’m trying to convert long 1099 to BigDecimal 10.99; This gives me 11.00: AFAIK this should work. What’s my bonehead error? Answer The error is that there’s a distinction between scale and precision. The constructor of MathContext accepts a precision, which is a total number of decimal digits on either side of the decimal point. (For example, the original BigDecimal

Advertisement