Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding MathContextin stream

I have this piece of code that sets a BigDecimal:

setTotalDogsCurrentValueInUsd(
            purchases
                .stream()
                .map(p -> p.getNumberOfDogs())
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add).multiply(new BigDecimal(rate)));

I would like to add MathContext.DECIMAL32 to round but I don't know where to add it exaclty

like image 565
Nunyet de Can Calçada Avatar asked Sep 24 '26 02:09

Nunyet de Can Calçada


1 Answers

use this overload of multiply:

.multiply(new BigDecimal(rate), MathContext.DECIMAL32)

or if you want to apply it during the reduce call:

.reduce(BigDecimal.ZERO, (a, b) -> a.add(b,  MathContext.DECIMAL32))
like image 171
Ousmane D. Avatar answered Sep 26 '26 16:09

Ousmane D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!