Workflow Soluiton

 View Only
  • 1.  Help with Javascript

    Posted Mar 01, 2011 09:15 AM

    I have a workflow that calculates the precentage of a wage change on a form.  When the user inputs the current pay and then the new pay it automatically calculates the precentage change and puts it in a new Labelcomponent field.  Everything works fine for numbers below 999.99 however once you go over 1000 it gives me a NaN.  I found this script in the newwage and currentwage components and I believe it's the problem.

    document.getElementById('lblWagePercent').innerText = ((igedit_getById('txtNewWage').text - igedit_getById('txtCurrentWage').text) / igedit_getById('txtCurrentWage').text) * 100;

     

    It's pretty obvious that its calculating the field but I think it's output is the wrong data type?   Any java experts out there that can help me with this?

    The 2 input fields are decimal fields since they represent dollar amounts.

     



  • 2.  RE: Help with Javascript

    Posted Mar 01, 2011 12:26 PM

    You could append a (..).toString() at the end of the right side OR String(...)  of everything on the right to ensure the output is a string.

    I'd also verify that you can never get a divide by zero with the current wage, AND that the new wage/current wage fields always have values in them (this would result in the not a number/NaN).

    What values were you using with the 999.99?



  • 3.  RE: Help with Javascript

    Posted Mar 01, 2011 01:27 PM

    thanks reecardo..  the 999.99 was an example.. the current wage it the employees current wage whether hourly or annually.   So for instance say employee has a current wage of 10.50 and the boss raises it to 11.00.  The precentage is displayed fine, however if the employee has a wage of say 35,000 and the boss wants to raise it to 35,000 then the precentage of the change comes out as Nan.   Anything that would throw in the coma like 1,000 seems to cause the issue.  If we use numbers below 1,000 it works fine.

    We actually tried both of those but it still gave the error. 

    I can fix the issue by adding a button to the form to calculate the percentage and thats what I've done for now it's just not dynamic..



  • 4.  RE: Help with Javascript

    Posted Mar 01, 2011 02:06 PM

    OK, now I get it... commas aren't acceptable as separators for numbers in javascript. So... you'd have to sanitize 10,000 somehow and make it 10000.

    You could do this a number of ways. Easiest way would be to do a txtblah.replace(',', '') (i.e. replace all commas with an empty space)

    So, we'd get something like this (untested)

    document.getElementById('lblWagePercent').innerText =

    ((igedit_getById('txtNewWage').text.replace(',', '') - igedit_getById('txtCurrentWage').text.replace(',', '')) /

    igedit_getById('txtCurrentWage').text.replace(',', '')) * 100;



  • 5.  RE: Help with Javascript

    Posted Apr 12, 2011 08:05 PM

    hey reccardo.. that works great.  Is there a way to round up the result to 2 dec places?



  • 6.  RE: Help with Javascript

    Posted Apr 13, 2011 12:22 PM

    You can try this (untested)

    //BEGIN

    var num = ((igedit_getById('txtNewWage').text.replace(',', '') - igedit_getById('txtCurrentWage').text.replace(',', '')) /

    igedit_getById('txtCurrentWage').text.replace(',', '')) * 100;

    var result = Math.round(num*Math.pow(10,2))/Math.pow(10,2);

    document.getElementById('lblWagePercent').innerText = result;

    //END

     

    num may have to be converted to a float from a string... if that's the case, use this:

    //BEGIN

    var num = parseFloat(((igedit_getById('txtNewWage').text.replace(',', '') - igedit_getById('txtCurrentWage').text.replace(',', '')) /

    igedit_getById('txtCurrentWage').text.replace(',', '')) * 100);

    var result = Math.round(num*Math.pow(10,2))/Math.pow(10,2);

    document.getElementById('lblWagePercent').innerText = result;

    //END



  • 7.  RE: Help with Javascript

    Posted Apr 13, 2011 01:11 PM

    reecardo YOUR GREAT!  thanks that worked!