## Forum Stats

• 3,876,242 Users
• 2,267,083 Discussions

Discussions

# Round up (Round.top) function in OIA

Member Posts: 2 Green Ribbon

In my OIA-model, I want the outcome of a certain rulebase to always round up to the nearest whole number instead of rounding down. For example 5.23, 5.44, 5.87 al have to become 6. And 150.2 and 150.9 have to become 151.

I can only find the function Round(), but it automatically rounds up or down depending which one is closest. Does anyone have a solution for this?

• Member Posts: 528 Bronze Trophy

Hi

You could use a combination of Trunc() and a comparison.

If your number X is 3.3, you create an attribute Y to store the Trunc(X, 0) version which gives us 3.

Then you substract X from Y. Anything more than a 0 as a result means you need to round up, so add one to the truncated value. You could also use Remainder to divide X by Y and if you get a remainder other than 0 you round up by 1.

Store that in your new attribute. There are probably a gazillion more elegant ways to do it, but that seems to fit your scenario. I may be wrong if I have misinterpreted it or am missing a piece.

R

• Member Posts: 4 Red Ribbon

Richard was faster than me, I was going to propose something similar (in french version, sorry) :

• Member, Moderator Posts: 310 Employee
edited Jun 9, 2021 3:38PM Answer ✓

You could also do this with a formula instead of a rule where your result is originally calculated (assuming you don't want both the actual and rounded value.

Round ((Trunc(X * 1000, 0) + 500 ) / 1000)

You can vary the "1000" and "500" to select the number of significant digits that cause "up-rounding" (i.e. to the third digit after the decimal in this case)

If it isn't critical to control the significant digits you consider you can simplify to Round (X + .49999... ) to get a very similar results)

In either case test the boundary conditions to get the exact results you want (i.e. .999.....9999, 1, 1..00000....00001 , 1.50000001 , etc to ensure you get the number of significant digits you require).

• Master Principal Solution Consultant Member Posts: 725 Employee

When I've needed to round up, it's typically been in the context of rounding up currency values. Here's an approach I've used for the following requirement...

Example source material

If the input amount is not already in whole dollars, round it up to the next whole dollar. For example:

• If the input amount is \$5, the result is \$5.
• If the input amount is \$5.01, the result is \$6.

Example rule

• the result = Trunc((the input amount + 0.99), 0)

Cheers,

Jasmine

• Member Posts: 528 Bronze Trophy

Hi

You could use a combination of Trunc() and a comparison.

If your number X is 3.3, you create an attribute Y to store the Trunc(X, 0) version which gives us 3.

Then you substract X from Y. Anything more than a 0 as a result means you need to round up, so add one to the truncated value. You could also use Remainder to divide X by Y and if you get a remainder other than 0 you round up by 1.

Store that in your new attribute. There are probably a gazillion more elegant ways to do it, but that seems to fit your scenario. I may be wrong if I have misinterpreted it or am missing a piece.

R

• Member Posts: 4 Red Ribbon

Richard was faster than me, I was going to propose something similar (in french version, sorry) :

• Member, Moderator Posts: 310 Employee
edited Jun 9, 2021 3:38PM Answer ✓

You could also do this with a formula instead of a rule where your result is originally calculated (assuming you don't want both the actual and rounded value.

Round ((Trunc(X * 1000, 0) + 500 ) / 1000)

You can vary the "1000" and "500" to select the number of significant digits that cause "up-rounding" (i.e. to the third digit after the decimal in this case)

If it isn't critical to control the significant digits you consider you can simplify to Round (X + .49999... ) to get a very similar results)

In either case test the boundary conditions to get the exact results you want (i.e. .999.....9999, 1, 1..00000....00001 , 1.50000001 , etc to ensure you get the number of significant digits you require).

• Master Principal Solution Consultant Member Posts: 725 Employee

When I've needed to round up, it's typically been in the context of rounding up currency values. Here's an approach I've used for the following requirement...

Example source material

If the input amount is not already in whole dollars, round it up to the next whole dollar. For example:

• If the input amount is \$5, the result is \$5.
• If the input amount is \$5.01, the result is \$6.

Example rule

• the result = Trunc((the input amount + 0.99), 0)

Cheers,

Jasmine