User Tools

Site Tools


programming:coldfusion:snippets:roundbetter

Programming - ColdFusion - Snippets - roundBetter

A better round function taken from Jamie Krug's ColdFusion Blog - ColdFusion round() function bug when operation performed on argument

<cffunction name="roundBetter" returntype="numeric" access="public" output="false">
    <cfargument name="toRound" type="numeric" required="true" />
    <cfargument name="scale" type="numeric" required="false" default="0"
                hint="Scale to round to (number of decimal places)." />
 
    <cfscript>
        var result = arguments.toRound;
        var scaleMultiplier = 10^arguments.scale;
 
        result = result * scaleMultiplier;
        result = round(javaCast('string', result) );
        result = javaCast('string', result/scaleMultiplier);
 
        return result;
    </cfscript>
</cffunction>
programming/coldfusion/snippets/roundbetter.txt · Last modified: 2019/03/12 10:30 by bas

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki