{"id":1674,"date":"2012-09-09T21:39:54","date_gmt":"2012-09-09T16:09:54","guid":{"rendered":"http:\/\/www.prasadgupte.com\/blog\/?p=1674"},"modified":"2012-09-09T22:01:40","modified_gmt":"2012-09-09T16:31:40","slug":"php-print-amount-number-in-words","status":"publish","type":"post","link":"https:\/\/www.prasadgupte.com\/blog\/php-print-amount-number-in-words\/","title":{"rendered":"PHP: Print amount or number in words"},"content":{"rendered":"<p>Its been real long since I updated this space. And instead of adding something for the heck of it, I thought of posting something really useful. This is a piece of PHP code that returns an amount\/number in words. This is in English, but you can easily translate it to your own. Hope this helps!<\/p>\n<p>This reminds me of Semester 1 assignment given by Shalini Puri ma&#8217;am to determine the optimal number of currency coins &amp; notes required to put together a specified amount. Good old days!<\/p>\n<pre class=\"theme:neon height-set:true toolbar:1 nums:false lang:php decode:true\" title=\"Class Money\">class Money {\r\n    \/\/ Uses the Thousand, Lakh, Crore format\r\n    CONST ThouLakhCrore = 0;\r\n    \/\/ Uses the Thousand, Million, Billion format\r\n    CONST ThouMilBil = 1;\r\n\r\n    public static function amountToWords($no, $currency='INR', $formatString = '%UNIT% %WHOLE% &amp;amp; %FRACTION% %SUBUNIT%', $option = self::ThouLakhCrore) {\r\n        if ($no == 0)\r\n            return \"Zero\";\r\n        $words = $formatString;\r\n        $whole = '';\r\n        $fraction = '';\r\n\r\n        $wholeInt = floor($no);\r\n        $whole = ($wholeInt == 0) ? 'Zero' : self::words_big($wholeInt, $option);\r\n        $fracInt = (int) (($no - $wholeInt) * 100) % 100;\r\n        $fraction = ($fracInt == 0) ? 'Zero' : self::words_tens($fracInt);\r\n\r\n        \/\/ TBD: Lookup unit, subunit for $currency & replace values below\r\n        $curr_unit = 'Rs.';\r\n        $curr_subunit = 'paise';\r\n\r\n        $words = str_replace('%WHOLE%', $whole, $words);\r\n        $words = str_replace('%FRACTION%', $fraction, $words);\r\n        $words = str_replace('%UNIT%', $curr_unit, $words);\r\n        $words = str_replace('%SUBUNIT%', $curr_subunit, $words);\r\n\r\n        return ucfirst($words);\r\n    }\r\n\r\n    protected static function words_big($no, $option) {\r\n        $descSingular = $option == 0 ? array(\"Thousand\", \"Lakh\", \"Crore\", \"Thousand Crore\") : array(\"Thousand\", \"Million\", \"Billion\", \"Trillion\");\r\n        $descPlural = $option == 0 ? array(\"Thousand\", \"Lakhs\", \"Crores\", \"Thousand Crores\") : array(\"Thousand\", \"Million\", \"Billion\", \"Trillion\");\r\n        $compare = $option == 0 ? array(1000, 100000, 10000000, 10000000000) : array(1000, 1000000, 1000000000, 1000000000000);\r\n\r\n        $divide = count($descSingular);\r\n        $loop = $divide;\r\n        $words = '';\r\n\r\n        \/* handle upto thousand *\/\r\n        for ($i = $loop - 1; $i &gt;= 0; $i--) {\r\n            $nos = ($i == $loop - 1) ? $no : $no % $compare[$i + 1];\r\n            $split = (int) floor($nos \/ $compare[$i]);\r\n\r\n            if ($split &gt; 0) {\r\n\r\n                if ($split &lt; 100)\r\n                    $words .= ' ' . self::words_tens($split) . ' ' . ($split == 1 ? $descSingular[$i] : $descPlural[$i]);\r\n                if ($split &gt;= 100 &amp;amp;&amp;amp; $split &lt; 1000)\r\n                    $words .= ' ' . self::words_hundreds($split) . ' ' . $descPlural[$i];\r\n                else if ($split &gt;= 1000)\r\n                    $words .= ' ' . self::words_big($split, $option) . ' ' . $descPlural[$i];\r\n            }\r\n            if ($i == 0) {\r\n                $hundred = (int) $no % 1000;\r\n                if ($hundred &gt; 0 &amp;amp;&amp;amp; $hundred &lt; 999)\r\n                    $words .= ' ' . self::words_hundreds($hundred);\r\n            }\r\n        }\r\n        return trim($words);\r\n    }\r\n\r\n    protected static function words_hundreds($no) {\r\n        $words = '';\r\n        if ($no &gt; 999)\r\n            throw new Exception(\"Invalid call to hundreds function\");\r\n        $hundreds = 0;\r\n        if ($no &gt; 99) {\r\n            $hundreds = floor($no \/ 100);\r\n            $words = self::words_units($hundreds) . ' ' . \"Hundred\";\r\n        }\r\n        $words .= ' ' . self::words_tens($no - (100 * $hundreds));\r\n        return trim($words);\r\n    }\r\n\r\n    protected static function words_tens($no) {\r\n        $words = '';\r\n        if ($no &gt; 99)\r\n            throw new Exception(\"Invalid call to Tens function\");\r\n        $tens = floor($no \/ 10);\r\n        if ($tens &gt; 1) {\r\n            switch ($tens) {\r\n                case 2:\r\n                    $words = \"Twenty\";\r\n                    break;\r\n                case 3:\r\n                    $words = \"Thirty\";\r\n                    break;\r\n                case 4:\r\n                    $words = \"Forty\";\r\n                    break;\r\n                case 5:\r\n                    $words = \"Fifty\";\r\n                    break;\r\n                case 6:\r\n                    $words = \"Sixty\";\r\n                    break;\r\n                case 7:\r\n                    $words = \"Seventy\";\r\n                    break;\r\n                case 8:\r\n                    $words = \"Eighty\";\r\n                    break;\r\n                case 9:\r\n                    $words = \"Ninety\";\r\n                    break;\r\n                default:\r\n                    break;\r\n            }\r\n            $words .= ' ' . self::words_units($no - $tens * 10);\r\n        }\r\n        else\r\n            $words = self::words_units($no);\r\n        return trim($words);\r\n    }\r\n\r\n    protected static function words_units($no) {\r\n        $words = '';\r\n        if ($no &gt; 19)\r\n            throw new Exception(\"Invalid call to Units function\");\r\n        switch ($no) {\r\n            case 1:\r\n                $words = \"One\";\r\n                break;\r\n            case 2:\r\n                $words = \"Two\";\r\n                break;\r\n            case 3:\r\n                $words = \"Three\";\r\n                break;\r\n            case 4:\r\n                $words = \"Four\";\r\n                break;\r\n            case 5:\r\n                $words = \"Five\";\r\n                break;\r\n            case 6:\r\n                $words = \"Six\";\r\n                break;\r\n            case 7:\r\n                $words = \"Seven\";\r\n                break;\r\n            case 8:\r\n                $words = \"Eight\";\r\n                break;\r\n            case 9:\r\n                $words = \"Nine\";\r\n                break;\r\n            case 10:\r\n                $words = \"Ten\";\r\n                break;\r\n            case 11:\r\n                $words = \"Eleven\";\r\n                break;\r\n            case 12:\r\n                $words = \"Twelve\";\r\n                break;\r\n            case 13:\r\n                $words = \"Thirteen\";\r\n                break;\r\n            case 14:\r\n                $words = \"Fourteen\";\r\n                break;\r\n            case 15:\r\n                $words = \"Fifteen\";\r\n                break;\r\n            case 16:\r\n                $words = \"Sixteen\";\r\n                break;\r\n            case 17:\r\n                $words = \"Seventeen\";\r\n                break;\r\n            case 18:\r\n                $words = \"Eighteen\";\r\n                break;\r\n            case 19:\r\n                $words = \"Nineteen\";\r\n                break;\r\n            default:\r\n                break;\r\n        }\r\n        return $words;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Its been real long since I updated this space. And instead of adding something for the heck of it, I thought of posting something really useful. This is a piece of PHP code that returns an amount\/number in words. This is in English, but you can easily translate it to your own. Hope this helps! &hellip; <a href=\"https:\/\/www.prasadgupte.com\/blog\/php-print-amount-number-in-words\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP: Print amount or number in words<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[728],"tags":[807,810,809,101,808],"class_list":["post-1674","post","type-post","status-publish","format-standard","hentry","category-code","tag-convert-amount-in-text","tag-display-money-format","tag-format-text-currency","tag-php","tag-print-number-in-words"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/posts\/1674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/comments?post=1674"}],"version-history":[{"count":0,"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/posts\/1674\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/media?parent=1674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/categories?post=1674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prasadgupte.com\/blog\/wp-json\/wp\/v2\/tags?post=1674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}