#Forms I'm using the following JavaScript to calculate a number in my form:

#Forms I'm using the following JavaScript to calculate a number in my form:
window.FormAPI.setValue('number10', (window.FormAPI.getValue("number8").id * window.FormAPI.getValue("radio9").id));
The value needs to be displayed with 2 decimal places 0.89 instead of 0.89999999. The value is calculated dynamically on the form. Is there a way I can do this in Appian v6.6.1 using JavaScript?
I tried FormAPI.setFormatValue('text11',(window.FormAPI.getValue("text11").id),#.##); on the event rules (load) for the form, and I'm getting an exception error. Thanks......

OriginalPostID-85278

OriginalPostID-85278

  Discussion posts and replies are publicly visible

Parents
  • If you want to use Patty's suggestion forum.appian.com/.../f-85275 this would look like this

    var operandOne = window.FormAPI.getValue("number8").id;
    var operandTwo = window.FormAPI.getValue("radio9").id;
    window.FormAPI.setValue('number10',(operandOne * operandTwo).toFixed(2));
  • It seems like Math.round() is a better solution, but it is not! In some cases it will NOT round correctly. Also, toFixed() will NOT round correctly in some cases.

    To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom JavaScript round function that performs a "nearly equal" test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

    Number.prototype.roundTo = function(decimal) {
    return +(Math.round(this + "e+" + decimal) + "e-" + decimal);
    }
    
    var num = 9.7654;
    console.log( num.roundTo(2)); //output 9.77

Reply
  • It seems like Math.round() is a better solution, but it is not! In some cases it will NOT round correctly. Also, toFixed() will NOT round correctly in some cases.

    To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom JavaScript round function that performs a "nearly equal" test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

    Number.prototype.roundTo = function(decimal) {
    return +(Math.round(this + "e+" + decimal) + "e-" + decimal);
    }
    
    var num = 9.7654;
    console.log( num.roundTo(2)); //output 9.77

Children