{"id":79,"date":"2010-03-26T11:33:20","date_gmt":"2010-03-26T15:33:20","guid":{"rendered":"http:\/\/www.joebillman.com\/blog\/?p=79"},"modified":"2010-03-26T11:33:20","modified_gmt":"2010-03-26T15:33:20","slug":"how-to-format-a-number-as-money","status":"publish","type":"post","link":"https:\/\/www.joebillman.com\/blog\/?p=79","title":{"rendered":"How to format a number as money"},"content":{"rendered":"<p>Recently I was working on a project where I had to take a number that represented a monetary value and display it as money. For example say I had a numeric variable that contained the value of 10000 and I needed it to display like money with the commas and dollar sign like this $10,000. So in other words I needed the string representation.\u00a0That is what this simple function that I am posting does. I have written it here in Flash ActionScript 3\u00a0as a static function of a class, but you could adapt it however you need to. Here is the code:<\/p>\n<pre style=\"background-color:#EBEBEB\">public class Formatter\n{\n    public static function formatMoney(val:int):String\n    {\n        var isNegative:Boolean = false;\n        var moneyVal:String = val.toString();\n        var len:uint;\n        if(val < 0)\n        {\n             isNegative = true;\n             moneyVal = moneyVal.replace(\/-\/, \"\");\n        }\n        len = moneyVal.length;\n        for(var i:int=len; i&gt;0; i-=3)\n        {\n            if(i == len)\n            {\n                continue;\n            }\n            var firstHalf:String = moneyVal.substring(0, i);\n            var lastHalf:String = moneyVal.substring(i);\n            moneyVal = firstHalf+\",\"+lastHalf;\n        }\n        if(isNegative)\n        {\n            return\"$-\"+moneyVal;\n        }\n        return \"$\"+moneyVal;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was working on a project where I had to take a number that represented a monetary value and display it as money. For example say I had a numeric variable that contained the value of 10000 and I needed it to display like money with the commas and dollar sign like this $10,000. &hellip; <a href=\"https:\/\/www.joebillman.com\/blog\/?p=79\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to format a number as money&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,9,27],"tags":[],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-actionscript","category-flash","category-programming"],"_links":{"self":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=79"}],"version-history":[{"count":0,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}