{"id":428,"date":"2019-04-04T00:26:45","date_gmt":"2019-04-03T18:56:45","guid":{"rendered":"https:\/\/webdesignerinc.com\/blog\/?p=428"},"modified":"2019-04-04T00:30:22","modified_gmt":"2019-04-03T19:00:22","slug":"convert-numbers-to-words-in-php","status":"publish","type":"post","link":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/","title":{"rendered":"Convert Numbers to Words in PHP"},"content":{"rendered":"<p>This script will convert numbers to words. It also contains an example.<\/p>\n<p>The function can accept big numbers also, both positive and negative.<\/p>\n<p>Enjoy coding.<\/p>\n<p><code>function convert_number_to_words($number) {<\/code><\/p>\n<p><code>$hyphen = '-';<br \/>\n$conjunction = ' and ';<br \/>\n$separator = ', ';<br \/>\n$negative = 'negative ';<br \/>\n$decimal = ' point ';<br \/>\n$dictionary = array(<br \/>\n0 =&gt; 'zero',<br \/>\n1 =&gt; 'one',<br \/>\n2 =&gt; 'two',<br \/>\n3 =&gt; 'three',<br \/>\n4 =&gt; 'four',<br \/>\n5 =&gt; 'five',<br \/>\n6 =&gt; 'six',<br \/>\n7 =&gt; 'seven',<br \/>\n8 =&gt; 'eight',<br \/>\n9 =&gt; 'nine',<br \/>\n10 =&gt; 'ten',<br \/>\n11 =&gt; 'eleven',<br \/>\n12 =&gt; 'twelve',<br \/>\n13 =&gt; 'thirteen',<br \/>\n14 =&gt; 'fourteen',<br \/>\n15 =&gt; 'fifteen',<br \/>\n16 =&gt; 'sixteen',<br \/>\n17 =&gt; 'seventeen',<br \/>\n18 =&gt; 'eighteen',<br \/>\n19 =&gt; 'nineteen',<br \/>\n20 =&gt; 'twenty',<br \/>\n30 =&gt; 'thirty',<br \/>\n40 =&gt; 'fourty',<br \/>\n50 =&gt; 'fifty',<br \/>\n60 =&gt; 'sixty',<br \/>\n70 =&gt; 'seventy',<br \/>\n80 =&gt; 'eighty',<br \/>\n90 =&gt; 'ninety',<br \/>\n100 =&gt; 'hundred',<br \/>\n1000 =&gt; 'thousand',<br \/>\n1000000 =&gt; 'million',<br \/>\n1000000000 =&gt; 'billion',<br \/>\n1000000000000 =&gt; 'trillion',<br \/>\n1000000000000000 =&gt; 'quadrillion',<br \/>\n1000000000000000000 =&gt; 'quintillion'<br \/>\n);<\/code><\/p>\n<p><code>if (!is_numeric($number)) {<br \/>\nreturn false;<br \/>\n}<\/code><\/p>\n<p><code>if (($number &gt;= 0 &amp;&amp; (int) $number &lt; 0) || (int) $number &lt; 0 - PHP_INT_MAX) {<br \/>\n\/\/ overflow<br \/>\ntrigger_error(<br \/>\n'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,<br \/>\nE_USER_WARNING<br \/>\n);<br \/>\nreturn false;<br \/>\n}<\/code><\/p>\n<p><code>if ($number &lt; 0) {<br \/>\nreturn $negative . convert_number_to_words(abs($number));<br \/>\n}<\/code><\/p>\n<p><code>$string = $fraction = null;<\/code><\/p>\n<p><code>if (strpos($number, '.') !== false) {<br \/>\nlist($number, $fraction) = explode('.', $number);<br \/>\n}<\/code><\/p>\n<p><code>switch (true) {<br \/>\ncase $number &lt; 21:<br \/>\n$string = $dictionary[$number];<br \/>\nbreak;<br \/>\ncase $number &lt; 100:<br \/>\n$tens = ((int) ($number \/ 10)) * 10;<br \/>\n$units = $number % 10;<br \/>\n$string = $dictionary[$tens];<br \/>\nif ($units) {<br \/>\n$string .= $hyphen . $dictionary[$units];<br \/>\n}<br \/>\nbreak;<br \/>\ncase $number &lt; 1000:<br \/>\n$hundreds = $number \/ 100;<br \/>\n$remainder = $number % 100;<br \/>\n$string = $dictionary[$hundreds] . ' ' . $dictionary[100];<br \/>\nif ($remainder) {<br \/>\n$string .= $conjunction . convert_number_to_words($remainder);<br \/>\n}<br \/>\nbreak;<br \/>\ndefault:<br \/>\n$baseUnit = pow(1000, floor(log($number, 1000)));<br \/>\n$numBaseUnits = (int) ($number \/ $baseUnit);<br \/>\n$remainder = $number % $baseUnit;<br \/>\n$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];<br \/>\nif ($remainder) {<br \/>\n$string .= $remainder &lt; 100 ? $conjunction : $separator;<br \/>\n$string .= convert_number_to_words($remainder);<br \/>\n}<br \/>\nbreak;<br \/>\n}<\/code><\/p>\n<p><code>if (null !== $fraction &amp;&amp; is_numeric($fraction)) {<br \/>\n$string .= $decimal;<br \/>\n$words = array();<br \/>\nforeach (str_split((string) $fraction) as $number) {<br \/>\n$words[] = $dictionary[$number];<br \/>\n}<br \/>\n$string .= implode(' ', $words);<br \/>\n}<\/code><\/p>\n<p><code>return $string;<br \/>\n}<br \/>\n<\/code><br \/>\nUsage<\/p>\n<p><code><br \/>\necho convert_number_to_words(123456789);<br \/>\n\/\/ one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine<\/code><\/p>\n<p><code>echo convert_number_to_words(123456789.123);<br \/>\n\/\/ one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine point one two three<\/code><\/p>\n<p><code>echo convert_number_to_words(-1922685.477);<br \/>\n\/\/ negative one million, nine hundred and twenty-two thousand, six hundred and eighty-five point four seven seven<\/code><\/p>\n<p><code>\/\/ float rounding can be avoided by passing the number as a string<br \/>\necho convert_number_to_words(123456789123.12345); \/\/ rounds the fractional part<br \/>\n\/\/ one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two<br \/>\necho convert_number_to_words('123456789123.12345'); \/\/ does not round<br \/>\n\/\/ one hundred and twenty-three billion, four hundred and fifty-six million, seven hundred and eighty-nine thousand, one hundred and twenty-three point one two three four five<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script will convert numbers to words. It also contains an example. The function can accept big numbers also, both positive and negative. Enjoy coding. function convert_number_to_words($number) { $hyphen =&#8230; <a class=\"read-more-link\" href=\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[25],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-tutorial","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Convert Numbers to Words in PHP - Web designer Blog | web designer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert Numbers to Words in PHP - Web designer Blog | web designer\" \/>\n<meta property=\"og:description\" content=\"This script will convert numbers to words. It also contains an example. The function can accept big numbers also, both positive and negative. Enjoy co\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Web designer Blog | web designer\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Webdesignerinc\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-03T18:56:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-03T19:00:22+00:00\" \/>\n<meta name=\"author\" content=\"Teji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@teji_tejinder\" \/>\n<meta name=\"twitter:site\" content=\"@teji_tejinder\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Teji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\"},\"author\":{\"name\":\"Teji\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7\"},\"headline\":\"Convert Numbers to Words in PHP\",\"datePublished\":\"2019-04-03T18:56:45+00:00\",\"dateModified\":\"2019-04-03T19:00:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\"},\"wordCount\":32,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\"},\"keywords\":[\"PHP\"],\"articleSection\":[\"Tutorial\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\",\"name\":\"Convert Numbers to Words in PHP - Web designer Blog | web designer\",\"isPartOf\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#website\"},\"datePublished\":\"2019-04-03T18:56:45+00:00\",\"dateModified\":\"2019-04-03T19:00:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webdesignerinc.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Convert Numbers to Words in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#website\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/\",\"name\":\"Web designer Blog | web designer\",\"description\":\"Web Designer and Developer Blog\",\"publisher\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webdesignerinc.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#organization\",\"name\":\"Web designer Blog | web designer\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png\",\"contentUrl\":\"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png\",\"width\":409,\"height\":80,\"caption\":\"Web designer Blog | web designer\"},\"image\":{\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Webdesignerinc\",\"https:\/\/x.com\/teji_tejinder\",\"http:\/\/www.linkedin.com\/in\/tejinder-singh-97bb19115\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7\",\"name\":\"Teji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png\",\"caption\":\"Teji\"},\"description\":\"i am admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert Numbers to Words in PHP - Web designer Blog | web designer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Convert Numbers to Words in PHP - Web designer Blog | web designer","og_description":"This script will convert numbers to words. It also contains an example. The function can accept big numbers also, both positive and negative. Enjoy co","og_url":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/","og_site_name":"Web designer Blog | web designer","article_publisher":"https:\/\/www.facebook.com\/Webdesignerinc","article_published_time":"2019-04-03T18:56:45+00:00","article_modified_time":"2019-04-03T19:00:22+00:00","author":"Teji","twitter_card":"summary_large_image","twitter_creator":"@teji_tejinder","twitter_site":"@teji_tejinder","twitter_misc":{"Written by":"Teji","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#article","isPartOf":{"@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/"},"author":{"name":"Teji","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7"},"headline":"Convert Numbers to Words in PHP","datePublished":"2019-04-03T18:56:45+00:00","dateModified":"2019-04-03T19:00:22+00:00","mainEntityOfPage":{"@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/"},"wordCount":32,"commentCount":0,"publisher":{"@id":"https:\/\/webdesignerinc.com\/blog\/#organization"},"keywords":["PHP"],"articleSection":["Tutorial"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/","url":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/","name":"Convert Numbers to Words in PHP - Web designer Blog | web designer","isPartOf":{"@id":"https:\/\/webdesignerinc.com\/blog\/#website"},"datePublished":"2019-04-03T18:56:45+00:00","dateModified":"2019-04-03T19:00:22+00:00","breadcrumb":{"@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webdesignerinc.com\/blog\/convert-numbers-to-words-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webdesignerinc.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Convert Numbers to Words in PHP"}]},{"@type":"WebSite","@id":"https:\/\/webdesignerinc.com\/blog\/#website","url":"https:\/\/webdesignerinc.com\/blog\/","name":"Web designer Blog | web designer","description":"Web Designer and Developer Blog","publisher":{"@id":"https:\/\/webdesignerinc.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webdesignerinc.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webdesignerinc.com\/blog\/#organization","name":"Web designer Blog | web designer","url":"https:\/\/webdesignerinc.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png","contentUrl":"https:\/\/webdesignerinc.com\/blog\/wp-content\/uploads\/2017\/09\/logo1.png","width":409,"height":80,"caption":"Web designer Blog | web designer"},"image":{"@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Webdesignerinc","https:\/\/x.com\/teji_tejinder","http:\/\/www.linkedin.com\/in\/tejinder-singh-97bb19115"]},{"@type":"Person","@id":"https:\/\/webdesignerinc.com\/blog\/#\/schema\/person\/5e2f01e80461d9a5ca3c361b86f405e7","name":"Teji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","url":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3933d592d4425623e44a3ddce18ae04f785295e0dd9b8b9d99251e81a0544a52?s=96&r=g&d=https:\/\/webdesignerinc.com\/blog\/wp-content\/plugins\/userswp\/assets\/images\/no_profile.png","caption":"Teji"},"description":"i am admin"}]}},"_links":{"self":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/comments?post=428"}],"version-history":[{"count":0,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/posts\/428\/revisions"}],"wp:attachment":[{"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/media?parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/categories?post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webdesignerinc.com\/blog\/wp-json\/wp\/v2\/tags?post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}