BigDecimal Round UP OR DOWN

2

I have these two scenarios working with bigdecimal:

BigDecimal result;

escenario 1: result = 19315859.60145000000000 ---[should be]--> 19315859.6014

escenario 2: result = 66841687.43665000000000 ---[should be]--> 66841687.4367

The problem is that I am using a variable (result) and if it is applied to this variable .setScale (4, BigDecimal.ROUND_HALF_UP) will work with scenario 2, but not with scenario 1, the same if I use .setScale 4, BigDecimal.ROUND_HALF_DOWN) scenario 1 will work but scenario 2 will not work .

What I want to do is that if the previous value is greater than 5, increase it by 1 and leave it as it is.

Ex: 
19315859.60145000000000 : ok the previous value is 4 so y leave it and the result should be 19315859.6014.

66841687.43665000000000 : ok the previous value is 6 so y increase 1 and the result should be  66841687.4367
    
asked by Martin 18.12.2016 в 18:38
source

1 answer

0

Good morning,

I would not use the rounding that comes by default in BigDecimal instead, I would use the RoundingMode of the java.lang API module that you can check here:

link

In your case, to use that rounding use the constant HALF-UP of this.

I recently implemented a virtual calculator in this way and it worked very well.

    
answered by 23.12.2016 в 22:22