This page shows samples of how my roundTo function for ColdFusion works.
|
|||||
| FUNCTION | value | RETURNS | |||
|---|---|---|---|---|---|
| roundTo(value,decimals,direction) | Returns Number | ||||
| roundTo(valueA,0) | 12.1 | 12 | |||
| roundTo(valueA,0,"Up") | 12.1 | 13 | |||
| roundTo(valueA,0,"Down") | 12.1 | 12 | |||
| roundTo(valueA,2,"R") | 12.1 | 12.1 | |||
| roundTo(valueA,2,"U") | 12.1 | 12.1 | |||
| roundTo(valueA,2,"F") | 12.1 | 12.1 | |||
| roundTo(valueB,0) | 12.25 | 12 | |||
| roundTo(valueB,0,"Up") | 12.25 | 13 | |||
| roundTo(valueB,0,"Down") | 12.25 | 12 | |||
| roundTo(valueB,2,"R") | 12.25 | 12.25 | |||
| roundTo(valueB,2,"U") | 12.25 | 12.25 | |||
| roundTo(valueB,2,"F") | 12.25 | 12.25 | |||
| roundTo(valueC,0) | 12.333 | 12 | |||
| roundTo(valueC,0,"Up") | 12.333 | 13 | |||
| roundTo(valueC,0,"Down") | 12.333 | 12 | |||
| roundTo(valueC,2,"R") | 12.333 | 12.33 | |||
| roundTo(valueC,2,"U") | 12.333 | 12.34 | |||
| roundTo(valueC,2,"F") | 12.333 | 12.33 | |||
| roundTo(valueD,0) | 12.500000 | 13 | |||
| roundTo(valueD,0,"Up") | 12.500000 | 13 | |||
| roundTo(valueD,0,"Down") | 12.500000 | 12 | |||
| roundTo(valueD,2,"R") | 12.500000 | 12.5 | |||
| roundTo(valueD,2,"U") | 12.500000 | 12.5 | |||
| roundTo(valueD,2,"F") | 12.500000 | 12.5 | |||
| roundTo(valueE,0) | 12.500001 | 13 | |||
| roundTo(valueE,0,"Up") | 12.500001 | 13 | |||
| roundTo(valueE,0,"Down") | 12.500001 | 12 | |||
| roundTo(valueE,2,"R") | 12.500001 | 12.5 | |||
| roundTo(valueE,2,"U") | 12.500001 | 12.51 | |||
| roundTo(valueE,2,"F") | 12.500001 | 12.5 | |||
| roundTo(valueF,0) | 12.67503 | 13 | |||
| roundTo(valueF,0,"Up") | 12.67503 | 13 | |||
| roundTo(valueF,0,"Down") | 12.67503 | 12 | |||
| roundTo(valueF,2,"R") | 12.67503 | 12.68 | |||
| roundTo(valueF,2,"U") | 12.67503 | 12.68 | |||
| roundTo(valueF,2,"F") | 12.67503 | 12.67 | |||
| roundTo(valueG,0) | 12.81674536 | 13 | |||
| roundTo(valueG,0,"Up") | 12.81674536 | 13 | |||
| roundTo(valueG,0,"Down") | 12.81674536 | 12 | |||
| roundTo(valueG,2,"R") | 12.81674536 | 12.82 | |||
| roundTo(valueG,2,"U") | 12.81674536 | 12.82 | |||
| roundTo(valueG,2,"F") | 12.81674536 | 12.81 | |||
Download roundTo() function:
<!--- =============== ROUNDTO() - ROUNDS A NUMBER TO A STATED NUMBER OF DECIMAL PLACES. =============== ---> <cffunction name="roundTo" returntype="numeric" output="no" hint="Rounds a number to a specified number of decimal places. It can also round up or down as specified."> <cfargument name="value" type="numeric" required="Yes"> <cfargument name="decimals" type="numeric" required="No" default="2"> <cfargument name="direction" type="any" required="No" default="Round"><!--- 1, Round, or R will round the number; 2, Up, U, Ceiling, or C will always round up; 3, Down, D, Fixed, or F will always round down. ---> <!--- ################################################### ## Author: George Jaros ## ## Script Name: ColdFusion roundTo Function ## ## Copyright 2006 George Jaros & Web 2 Market ## ## www.georgejaros.com www.web2market.com ## ## This code may be replicated as long as ## ## this header statement is included. The ## ## Instructions below may be removed. ## ################################################### ---> <CFSWITCH expression="#UCase(direction)#"> <CFCASE value="1,ROUND,R"> <CFRETURN Round(value*(10^decimals))/(10^decimals)> </CFCASE> <CFCASE value="2,UP,CEILING,U,C"> <CFRETURN Ceiling(value*(10^decimals))/(10^decimals)> </CFCASE> <CFCASE value="3,DOWN,FIXED,D,F"> <CFRETURN Int(value*(10^decimals))/(10^decimals)> </CFCASE> </CFSWITCH> </cffunction> <!--- =============== END ROUNDTO() =============== ---> |
|||||