Math formula to round numbers to ceiling

HI, so i need to round numbers up to the nearest full integer. So for instance if my number is 1.2 it has to round up to 2. But i noticed that with the “round” formula it rounds down to the nearest integer which would be 1. But in my use case this would lead to wrong calculations.

Is there a way to round any number with decimals up to the ceiling??

Thanks.

Math.ceil(1.2)

1 Like

Thanks Dorilama, i was hoping i could be done with formulas from weweb themselves. This looks easy enough to do, i’ll try this.

1 Like

I’d love rounddown and roundup formulae within WeWeb, too. I need to round the results of a complex formula.

Using Math.ceil works just fine in the formula view - and doesn’t particularly add complexity or unreadability to the result. All weweb formulas are wrappers for javascript functions, and they do their jobs best when they are focused on weweb-specific concerns and let the easy parts of JS do their job.

2 Likes

Nice, thanks! I didn’t know they could be combined like that.

Would I need to write a custom JS formula to round up to the nearest 10 or 100 instead of the nearest integer?

I would just do (for rounding to the nearest 10)

Math.round([myvalue]/10)*10

In the above, 993 would become 990

1 Like

Thanks, Ray - much appreciated!

I saw your business through your profile, too - awesome idea. I can see that being helpful to many.

Interesting, does that mean that we can use javascript functions in the formula field?

Yes, formula are javascript expression, any valid js will work :slight_smile:

1 Like

Awesome! Can we use arrow functions? Then we would have access to some of the array functions like reduce, map etc which would be incrediby powerfull.

Sure, just know that a formula is a javascript expression. If you want to do complexe stuff, you can go the the ‘js’ version which allow you to have intermediate variable etc (you just need to return the final result)

But something like myVar.map(item => ({label: item.label}) will totally work.

Small disclaimer, be aware that we are working in Vue. If you are doing complex logic, this will be rerun each time one of its dependencies change.

Also avoid anything mutative, like .sort for example (our implementation is safe)