rounding number to a decimal place

68 views
Skip to first unread message

Elisheva Buchsbaum

unread,
Dec 30, 2020, 2:48:20 AM12/30/20
to Kogito development mailing list
Hi, 
I am trying to round numbers to 2 decimal places in a consistent way.
We have discovered that the decimal() function does what's called "bankers rounding":
If I am trying to round a number 1.xx5 to 2 decimal points - then it makes a difference what the second decimal point is. If it's odd (1.035) it will get rounded up (1.04), but if it's even (1.065) it will get rounded down (1.06). 

This is only an issue when the number ends in 5. 

Is there a way to always round up in these cases? I don't want to ALWAYS round up (if the number i'm trying to round is 1.034. - I want it to be 1.03), just when the number i'm trying to round ends in 5. 

Thanks, 
Elisheva

Matteo Mortari

unread,
Jan 4, 2021, 3:52:39 AM1/4/21
to Kogito development mailing list
Hi Elisheva,
thank you for reporting.

The behaviour is intended: we implemented the DMN spec "decimal()" function internals with BigDecimal.ROUND_HALF_EVEN since this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.

You can achieve your desired customized rounding by invoking a through BKM a Java function call, to a static method performing something ~like:
    public static BigDecimal customRounding(BigDecimal n, BigDecimal scale) {

        return n.setScale( scale.intValue(), BigDecimal.ROUND_HALF_UP );

    }


If you have a use-case you can share in general terms, where it shows when the alternative rounding is meaningful, I'll be happy to hear and provide an extended built-in FEEL function that implements this directly out of the box... but I need a use-case to document where this can be helpful to people ;)

I hope this helps, and please let us know!
MM

--
You received this message because you are subscribed to the Google Groups "Kogito development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kogito-developm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kogito-development/4f5298c9-05de-4587-99ce-b718c953d5a1n%40googlegroups.com.

Elisheva Buchsbaum

unread,
Jan 4, 2021, 7:13:12 AM1/4/21
to kogito-de...@googlegroups.com
Hi Matteo,

Thanks! I will discuss this with my team.

Elisheva

You received this message because you are subscribed to a topic in the Google Groups "Kogito development mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kogito-development/TrvQStvtVwU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kogito-developm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kogito-development/CAHOoiEpwM8w10Pm2RBZza9evw0_0GN4Xta-n_iZDkY27pMAnDg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages