CURRENT PROJECTS
loading
'$' + (parseFloat('123.2')||0).toFixed(2)instead. I've been using this as of late and forgot to update this post a while ago.
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; } }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.
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; } }
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"