"rounding to" inconsistencies

Hello everyone,
please I need help understand what’s causing inconsistencies in my use of “:rounded to”.

It is used in my Stripe workflow to calculate a commission fee, including stripe fees:

x = total price
commission in cents = (x * 0.15 + x * 0.0175 + 0.25 )* 100

in bubble the formula i used is ( ( (Input A's value * 0.15) + (Input A's value *0.0175) ) + 0.25) * 100

I then apply a :rounded to 0

However if I have x = 22
then commission in cents = 393.5
and rounded to 0 gives 393.

however if I give a hardcoded 393.5 and rounded to 0, it returns 394.

Thanks a lot for your insights!

Rounding to 0 means the value will return 0 decimal values. Is that what you are looking to do?

If you want to avoid inconsistencies use ..floor or …ceil

Yes it needs to be rounded because it’s already been multiplied by 100 to obtain a value in cents to pass to Stripe API which expects an integer.

Floor or ceiling would create incorrect rounding as 0.9 would become 0 or 0.1 become 1 in some cases.

The documentation states that 0.5 should round up, which I’m fine with.
My problem has stated in the first message, is that sometimes it doesn’t apparently…

It’s a binary floating point precision error.

The actual, correct, mathematical answer to your calculation is indeed 393.5

But..

Because some numbers cannot be accurately represented in binary (without repeating infinitely), the computed answer is actually 393.49999999999994 (bubble just rounds it to 1 decimal place by default, giving 393.5).

so when you round it to 0 decimal places, you’re rounding the actual calculated value (393.49999999999994) which is why you get 393 and not 394.

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.