- Wed Jan 11, 2017 5:20 pm
#788418
So I've been working on setting a value to round to the nearest 5 or to the nearest 10 of another value and thought I'd share what I ended up with.
Here's how I did it in a trigger:
Trigger:
If you need it to round to the nearest 10, just change the 5s with 10s.
Hopefully this helps someone in the future trying to do the same.
Here's how I did it in a trigger:
Code: Select all
Here's how I did in in Javascript:
$roundContact = ceil((@#textVar003) / 5) * 5;
@@textVar004=$roundContact;
Code: Select all
You can also add numbers before rounding but for me it was tricky so I'll add examples of those too:function roundUp() {
$("#textVar003").setValue(Math.ceil(Number($("#country").getValue()) /5)*5);
}
$("#country").setOnchange(roundUp); // on change
roundUp($('#country').getValue(), ''); // on Load
Trigger:
Code: Select all
Javascript:
$roundContact = ceil((@#textVar003 + 3) / 5) * 5;
@@textVar004=$roundContact;
Code: Select all
If anyone has a cleaner way of doing this, please feel free to share. I'm a super newbie so I make no guarantees.function roundUp() {
$("#textVar003").setValue(Math.ceil((Number($("#country").getValue()) + 2.5)/5)*5);
}
$("#country").setOnchange(roundUp); // on change
roundUp($('#country').getValue(), ''); // on Load
If you need it to round to the nearest 10, just change the 5s with 10s.
Hopefully this helps someone in the future trying to do the same.