9.22.06 Flash & JavaScript dollarFormat function

UPDATE 8/15/2008 - For JavaScript - use '$'+(parseFloat('123.2')||0).toFixed(2) instead. I’ve been using this as of late and forgot to update this post a while ago.

UPDATE 2/18/2008 - Now works with negative values and properly formats ‘-.006′ as ‘-$0.01′ with cent rounding via Math.round() at precision level 2.

A quick function that was put together to parse strings/numbers (float as well) into a US Dollar formatted string. Case in point - we want the number 10000 to actually be turned into a string such as “$10,000.00″. This is a common function found in ColdFusion, PHP and the like. The function as follows is not anywhere as robust as it is in other languages - but it’s a good start for our entirely USD-based e-commerce applicaitons.

Flash version:

function dollarFormat (origNumber):String {
    var procNum:Number = 0;
    if (typeof origNumber == "string")
        procNum = parseFloat(origNumber);
    else if (typeof origNumber == "number")
        procNum = origNumber;
    else
        return "";
    var isNegative:Boolean = (procNum < 0) ? true : false;
    procNum = Math.abs(procNum);
    var numStr:String = (Math.round(procNum * 100)).toString();
    if (numStr.length == 0 || numStr == "0") {
        return ((isNegative) ? "-" : "") + "$0.00";
    } else if (numStr.length == 1) {
        return ((isNegative) ? "-" : "") + "$0.0" + numStr;
    } else if (numStr.length == 2) {
        return ((isNegative) ? "-" : "") + "$0." + numStr;
    } else {
        var dollarStr:String = numStr.substring(0, (numStr.length - 2));
        var centStr:String = numStr.slice(-2);
        var dollarOutStr:String = "";
        for (var charItr = dollarStr.length; charItr >= 0; charItr--) {
            dollarOutStr = ((((dollarStr.length - charItr) % 3) == 0 && dollarStr.length != charItr && charItr != 0) ? "," : "") + dollarStr.charAt(charItr) + dollarOutStr;
        }
        return ((isNegative) ? "-" : "") + "$" + dollarOutStr + "." + centStr;
    }
}

Download this code: dollarFormat-snippet

this can be simply called in an external .as file to be used as an #include or as a static function in an imported actionscript class.

JavaScript version:

function dollarFormat (origNumber) {
    var procNum = 0;
    if (typeof(origNumber) == 'string')
        procNum = parseFloat(origNumber);
    else if (typeof(origNumber) == 'number')
        procNum = origNumber;
    else
        return '';
    var isNegative = (procNum < 0) ? true : false;
    procNum = Math.abs(procNum);
    var numStr = (Math.round(procNum * 100)).toString();
    if (numStr.length == 0 || numStr == '0') {
        return ((isNegative) ? '-' : '') + '$0.00';
    } else if (numStr.length == 1) {
        return ((isNegative) ? '-' : '') + '$0.0' + numStr;
    } else if (numStr.length == 2) {
        return ((isNegative) ? '-' : '') + '$0.' + numStr;
    } else {
        var dollarStr = numStr.substring(0, (numStr.length - 2));
        var centStr = numStr.slice(-2);
        var dollarOutStr = '';
        for (var charItr = dollarStr.length; charItr >= 0; charItr--) {
            dollarOutStr = ((((dollarStr.length - charItr) % 3) == 0 && dollarStr.length != charItr && charItr != 0) ? "," : "") + dollarStr.charAt(charItr) + dollarOutStr;
        }
        return ((isNegative) ? '-' : '') + '$' + dollarOutStr + '.' + centStr;
    }
}

Download this code: dollarFormat-js-snippet

Here’s a little test output:

trace(this.dollarFormat(".004"));   // outputs "$0.00"
trace(this.dollarFormat(".005"));   // outputs "$0.01"
trace(this.dollarFormat(".014"));   // outputs "$0.01"
trace(this.dollarFormat(".015"));   // outputs "$0.02"
trace(this.dollarFormat(".019"));   // outputs "$0.02"
trace(this.dollarFormat("000.123"));    // outputs "$0.12"
trace(this.dollarFormat("1.234"))// outputs "$1.23"
trace(this.dollarFormat("12"));     // outputs "$12.00"
trace(this.dollarFormat("123"));    // outputs "$123.00"
trace(this.dollarFormat("1234"));   // outputs "$1,234.00"
trace(this.dollarFormat("12345"))// outputs "$12,345.00"
trace(this.dollarFormat("123456"))// outputs "$123,456.00"
trace(this.dollarFormat("123457"))// outputs "$123,457.00"
trace(this.dollarFormat("1234578"));    // outputs "$1,234,578.00"
trace(this.dollarFormat("12345789"));   // outputs "$12,345,789.00"

Download this code: dollarFormat-output


No comments




0.959s