• Resolved Helen

    (@web09)


    Hello,
    I’ve created a calculator with the Calculated Field Form plugin and used the PREC equation to round the result to 2 decimal places. It works fine, however it shows the ending zeroes when the number is exact.
    For example, 30.78 will show correctly as 30.78, but instead of showing 30.70 or 30.00, I want the calculator to round it up to 30.7 or 30 and ignore the zeros. Is there any way to remove the insignificant zeros? I want to keep 2 decimal places for the rest of course

    Thank you,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello,

    In this case, you should to check the number of decimals places, and use the PREC operation only if needed.

    For example, assuming your current equation is: PREC(fieldname1+fieldname2, 2)

    Edit it as follows:

    
    (function(){
        function countDecimals (value) { 
            if ((value % 1) != 0) 
                return value.toString().split(".")[1].length;  
            return 0;
        };
    
        var result = fieldname1+fieldname2;
       
        if(2<countDecimals(result)) return PREC(result,2);
        return result;
    })()
    

    Best regards.

    Thread Starter Helen

    (@web09)

    It worked, thank you for your help

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Remove insignificant trailing zeros CFF’ is closed to new replies.