CFLib.org – Common Function Library Project

DecimalFormatCorrectly(number)

Last updated October 12, 2004

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
DecimalFormat incorrectly rounds certain numbers, e.g. alterating between rounding up/down the same number at different times. This UDF is an attempt to correct that.

Return Values:
Returns a number.

Example:

view plain print about
<cfset Total = 599>
<cfset VAT = Total * 0.175>
<cfset GrandTotal = Total + VAT>

<cfoutput>
Using DecimalFormat (proof of the bug): <br>
Total: #DecimalFormat(Total)#<br>
Vat: #DecimalFormat(VAT)#<br>
Grand Total: #DecimalFormat(GrandTotal)#<br>
#DecimalFormat(104.825)#<br>
#DecimalFormat(703.825)#<br> 
<br>
Using DecimalFormatCorrectly:<br>
Total: #DecimalFormatCorrectly(Total)#<br>
Vat: #DecimalFormatCorrectly(VAT)#<br>
Grand Total: #DecimalFormatCorrectly(GrandTotal)#<br>
#DecimalFormatCorrectly(104.825)#<br>
#DecimalFormatCorrectly(703.825)#<br>
</cfoutput>

Parameters:

Name Description Required
number The number to format. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Corrects rounding bug in DecimalFormat.
 * Minor update.
 * 
 * @param number      The number to format. (Required)
 * @return Returns a number. 
 * @author duncan cumming (duncan.cumming@alienationdesign.co.uk) 
 * @version 2, October 12, 2004 
 */

function DecimalFormatCorrectly(number) {
    var lhs=0;
    var rhs=0;
    var decLen = 0;
    var i = 0;
    
    if(ListLen(number, "."EQ 2) {    //xxx.xxx
        lhs = ListFirst(number, ".");
        rhs = ListLast(number, ".");
    } else if (Find("."Trim(number)) EQ 1) { // .xx
        rhs = number;
    } else if (Find("."Trim(number)) EQ 0) {    // xx
        lhs = number;
    } else {
        return number;
    }
    
    if (NOT IsNumeric(lhs) OR NOT IsNumeric(rhs)) return number;
    
    for (i = 0; i LT 2; i=i+1) {
        if (Len(rhs) LT 2) rhs = rhs & "0";
    }
    
    // count how many digits > 2dp there are
    decLen = Len(rhs) - 2; 

    // divide by this number of zeroes
    for (i = 0; i LT decLen; i=i+1) {
        rhs = rhs / 10;
    } 

    // round it
    rhs = Round(rhs);

    if (rhs GTE 100) { 
        rhs = 0;
        lhs = lhs + 1;
    }

    // pad with zeros if necessary
    if (rhs LT 10) {
        rhs = "0" & rhs;
    }    
    
    lhs = NumberFormat(lhs);
    
    return (lhs & "." & rhs);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson