Is there a way, within a form (i.e., at runtime), to truncate a decimal number?
It’s a currency amount, so I want only two decimal places. But I don’t want to round the number. In other words, the result needs to looks like this:
1056.2304 = 1056.23
1056.2384 = 1056.23
Rounding the number can occasionally create a difference of $0.01. A penny may not seem like much but in this case it's important. This calculation is for determining the gross amount of a distribution when you know the net amount you want to receive after tax withholding is done.
Of course I’m sure there’s a RegEx to do this, but I need the truncation to happen on the form, not during the workflow.
I am happy to help, as I am sure all the community is. I know you already have the variables set up to do the calculation, but the issue is that those variables are rounding your result, so the damage is already done before it is output.
Using the same form as I provided but updating it with the Jquery below will give you the correct result. You just put in the amount the customer wants to receive, and the calculation is done for you with no rounding. If the withholding is always 4.75% you could hard code this amount and have one less field.
Interesting use case you have here. I tried messing with the form functions but had issues with the replace function. The only other solution I could think of was to create a form plugin, but then I remembered that there is a plugin in the gallery that lets you inject Jquery. https://gallery.nintex.com/t/js-insert
You add controls to your form and give them a CSS class so that JQuery can select them.
The code below will take the currency value from a control with the CSS class of c1. Calculate a percentage based on a number control c3. Then output a truncated (not rounded) value to c2.
This code may not be exactly what you need, but it will give you an idea of what is possible.
$(".c1 input, .c3 input").on("input", function() {let num = parseFloat($(".c1 input").val());
let percentage = parseFloat($(".c3 input").val());
if (!isNaN(num) && !isNaN(percentage)) {
let calculatedValue = num * (percentage / 100);
let truncatedValue = Math.trunc(calculatedValue * 100) / 100;
$(".c2 input").val(truncatedValue);
} else {
$(".c2 input").val("");
}
});
I've attached the form example, but you will need the plugin.
Wow, thanks, @SimonMuntz! You’re going above and beyond the call for me lately.
My JavaScript skills are pretty limited, and pretty rusty to boot. But I can usually tease out the meaning of most of it. It looks like your script is doing the calculation in addition to providing a result.
Could I ask what the code would look like if I already have the calculation done? I have some variables set up to do that, and the math is correct - except for those instances where, due to rounding, the result should be $0.01 different to get the correct net amount. For example, if the customer says they want to receive $2,000.00 after 4.75% withholding is done, the calculation result will be $2,099.74 ($2,099.7375 if rounded to four decimal places instead of two). But when the user starts the transaction with this gross amount of $2,099.74 (and they must start with the gross amount, there’s no other option), the net result will actually be $2,000.01. A lot of customers may not care if they receive that extra penny, but it would be nice if we could give them what they’ve actually requested.
If this is asking too much, and you have other fish to fry, just let me know. I don’t want to be presumptuous by asking for even more of your time. :)
I am happy to help, as I am sure all the community is. I know you already have the variables set up to do the calculation, but the issue is that those variables are rounding your result, so the damage is already done before it is output.
Using the same form as I provided but updating it with the Jquery below will give you the correct result. You just put in the amount the customer wants to receive, and the calculation is done for you with no rounding. If the withholding is always 4.75% you could hard code this amount and have one less field.
I’m getting a different result when I apply this script. Just to be sure it’s not interfering with something else in my form, I’ve started a new form to test it. This is what I’m getting:
I’ve copied and pasted your script into the js-insert field, so I know I didn’t mistype anything (I am assuming that “inout” after the else statement should be “input”). For the CSS classes, the net amount field is c1, the percentage field is c3, and the gross amount field is c2.
It looks like the decimal in the result is off by a couple of places (actually, three places).
(Incidentally, no, the withdrawal amount is not always 4.75%. It’s just what I’m using because I know the result is off by $0.01 if not truncated.)
The typo you picked up doesn’t really affect the code, as it only triggers when the input is not a number. The issue is that the withholding control type is set as an Integer, change it to decimal and it will work as expected. Forgot to mention I changed that.
Thank you. Now I see what the difference is. It appears that the JS will work only if the percentage field has a default value. If that is removed, or if the amount in the field is changed, the gross amount will be incorrect (off by the three decimal places).
Unfortunately, I actually have two percentage fields (one for state and one for federal), and neither can have a default value. (I didn’t mention the two fields previously because I didn’t think it was relevant.) I tried creating a hidden field whose value is the sum of the two percentages, and then giving that field the c3 class for the calculation, but it didn’t work.
Anyway, thank you for all your time on this. You don’t have to do any more work on it. I have some “disclaimer” text on the form noting that the gross amount may have to be adjusted by one cent in order to arrive at the correct net amount, and we can live with that.
Glad you have found a solution. I am currently creating a Nintex University course for form plugins so I thought this would be a great opportunity for me to learn how to output an object from a plugin. It’s ok if you do not want it or use it.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.