38
Views
10
Solved
###### Math Round functionality
Question

Hi All,

How do I round a number down to the closest integer in OutSystems without using JavaScript Math.Floor() ?

My case: I have an amount of days (using DiffDays) and need to substract the weekends. So if the difference between dates is 33 days, there are 4 weekends in between and the amount of working days is 33 - 8 = 25. The round method can not be set to round down at all times. The dates are added by the user and the working days in between should show up immediately.

Any way to still just to the calculations in the expression used to display the working days?

Regards,
Max

MVP
Solution

Working day calculations should also account for public holidays, it's not enough to just get rid of weekends. Also, your calculation is flawed: if you take a span of Monday through the Friday of next week, that's 12 days, with only one weekend in between. But if you take a span of 12 days from Saturday to the Wednesday of the second week, there's two weekends in between (but your calculation would yield only one). And if you start on Sunday, there's only 1.5 weekend (three days).

That said, if you assign a Decimal value to an Integer variable, you automatically get a truncated/floored result, you do not need a special function for that.

Solution

Max, if you just want to truncate the decimal part, use Trunc function.

Trunc(Decimal)Returns the Decimal number 'n' truncated to integer removing the decimal part of 'n'.

Hi Max,

I don't understand the relationship between Floor() and weekends.

You can use this function to determine which weekday is a given date:

DayOfWeek(DateTime) - Returns the week day of 'dt', ranging from 0 (Sunday) to 6 (Saturday).

Hi Jose,

I don't need to get the DayOfWeek from my date.
What I mean is that I want to know how many WORKING days are inbetween 2 dates. So this means getting rid of all weekends. For as far as I know OutSystems does not have a standard functionality to do this, right?

So what I thought is that if you calculated the amount of days and divide by seven you will have the amount of weeks past. The amount of weekends that have occured is always the Floor() number. For instance, 33 days between two dates means that 4,7 weeks have past. That would be equal to 4 weekends. So the Floor() logic needs to rounds 4,7 to 4. By that I can multiple by 2 and substract it from the days coming from DiffDay to know the working days.

MVP
Solution

Working day calculations should also account for public holidays, it's not enough to just get rid of weekends. Also, your calculation is flawed: if you take a span of Monday through the Friday of next week, that's 12 days, with only one weekend in between. But if you take a span of 12 days from Saturday to the Wednesday of the second week, there's two weekends in between (but your calculation would yield only one). And if you start on Sunday, there's only 1.5 weekend (three days).

That said, if you assign a Decimal value to an Integer variable, you automatically get a truncated/floored result, you do not need a special function for that.

Hi Kilian,

Thanks for your reply! I am aware of the fact that holidays are not taken into account. This is not needed since we work with a lot of off shore people who have different holidays. I have already set the date input field so that users are not able to select a weekend day (which they probably even would never do since they all know that working days should be chosen as start days and end dates).

I already fixed this issue by using a SQL statement with a FLOOR in it, but will defitenly give your solution a try! Should be quicker that way ;)

MVP

Glad I could be of help :). Happy coding!

Hi Max,

It is unclear how you are using Math functions with Weekends logic but,
for your question related to rounding a number to near integer, you can always use Math client & server functions without using javascripts.

Following is a quick link for Math Round in OutSystems.
https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Logic/Built-in_Functions/Math#Round

Regards,
Palak Patel

Hi Palak,

The round function is not able to always round to the lower integer, correct?

So if the outcome is 4.9 I want round to make it 4, not 5.

Hi Max,
Yes ceiling and floor are not currently built in functions as per in this idea:
https://www.outsystems.com/ideas/678/built-in-funtions
I can see that previously it was also pointed out but still not available. Let's hope it will be added soon.

Meanwhile I saw one component - MathUtils. Take a look. Its having MathFloor.
You can also create a small code with C# and wrap in your environment extension.

Thanks & Regards,
Palak Patel

Solution

Max, if you just want to truncate the decimal part, use Trunc function.

Trunc(Decimal)Returns the Decimal number 'n' truncated to integer removing the decimal part of 'n'.

MVP

Hello.

I answer this question every other month.

Use DiffDays to get how many days exist between the two dates. Do Trunc of the difference by 7 to get the number of full weeks.

`Weeks = Trunc(DiffDays(Date2,Date1)/7)`

Now you have saved a few calculations.

For the final 0 to 6 days, just test if they are working days or weekends and add them to whichever variable is suitable.

Advance to the next day and repeat.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.