Miscellaneous Code
  Home arrow Miscellaneous Code arrow Num2Words
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

Num2Words
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-11-05

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    With this class you can convert your integer/number to english fraction words. Ex. 1=>One, 250=>Two hundred fifty, 1000=>One thousand. Give it a try.

    By : hermawan

    <?php
    /***********************************************
    * Snippet Name : Num2Words *
    * Scripted By : Hermawan Haryanto *
    * Website : http://hermawan.com *
    * Email : hermawan@codewalkers.com *
    * License : GPL (Gnu Public License) *
    ***********************************************/
    // START CONVERTION CLASS
    class num2words {
    var $numb = Array();
    var $tail;
    var $number;
    var $currency;
    var $min;
    function num2words () {
    $this->numb = Array ("",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine");
    }
    function mod($a,$b) {
    return $a-$b*floor($a/$b);
    }
    function setTail($str) {
    $this->tail = $str;
    }
    function setNumber($int) {
    $int = trim($int);
    if (is_int(strpos($int,"-"))) {
    $this->number = substr($int,strpos($int,"-")+1,strlen($int));
    $this->currency = "minus";
    } else {
    $this->number = $int;
    }
    $this->setAsCurrency();
    }
    function getCurrency() {
    return $this->currency;
    }
    function printCurrency() {
    print ucfirst(strtolower(trim($this->currency)));
    }
    function setAsCurrency() {
    $xpos = strpos($this->number,".");
    if (is_int($xpos)) {
    $pecahan = round(substr($this->number,$xpos,strlen($this->number)),2);
    $sisa = substr($this->number,0,$xpos);
    } else {
    $pecahan = "";
    $sisa = $this->number;
    }
    if ($sisa==0 || $this->number==0) {
    $this->currency .= "zero ".$this->tail;
    } else {
    $trilion = floor($sisa/pow(10,12));
    $sisa = $this->mod($sisa,1000000000000);

    $billion = floor($sisa/pow(10,9));
    $sisa = $this->mod($sisa,1000000000);

    $million = floor($sisa/pow(10,6));
    $sisa = $this->mod($sisa,1000000);

    $thousand = floor($sisa/pow(10,3));
    $sisa = $this->mod($sisa,1000);

    $words = $this->ThreeDigit($trilion, "trilion");
    $words .= $this->ThreeDigit($billion, "billion");
    $words .= $this->ThreeDigit($million, "million");
    $words .= $this->ThreeDigit($thousand, "thousand");
    $words .= $this->ThreeDigit($sisa,"");
    $words .= " ".$this->tail;
    }
    if ($pecahan>0) {
    $words .= " and". $this->ThreeDigit(round($pecahan*100),"sen");
    }
    $this->currency .= strtolower($words);
    }
    function ThreeDigit($amount, $suffix="") {
    $sisa = (int) $amount;
    $words = "";
    if ($sisa < 20 && $sisa > 10) {
    if ($sisa==11) {
    $words = " eleven";
    } elseif ($sisa == 12) {
    $words = " twelve";
    } elseif ($sisa == 13) {
    $words = " thirteen";
    } elseif ($sisa == 15) {
    $words = " fifteen";
    } elseif ($sisa == 18) {
    $words = " eighteen";
    } else {
    $words = " ".$this->numb[$sisa-10]."teen";
    }
    if ($suffix != "") {
    $words .= " ".$suffix;
    }
    return $words;
    }
    $ratus = floor($sisa/100);
    if ($ratus <= 0) {
    $words .= "";
    } else {
    $words .= " ".$this->numb[$ratus]." hundred";
    }
    $sisa = $this->mod($sisa,100);
    if ($sisa < 20 && $sisa > 10) {
    if ($sisa == 11) {
    $words .= " eleven ". $suffix;
    } elseif ($sisa == 12) {
    $words .= " twelve";
    } elseif ($sisa == 13) {
    $words .= " thirteen";
    } elseif ($sisa == 15) {
    $words .= " fifteen";
    } elseif ($sisa == 18) {
    $words .= " eighteen";
    } else {
    $words .= " ".$this->numb[$sisa-10]."teen ". $suffix;
    }
    return $words;
    }
    $puluh = floor($sisa/10);
    if ($puluh == 0) {
    $words .= "";
    } elseif ($puluh == 1) {
    $words .= " ten";
    } elseif ($puluh == 2) {
    $words .= " twenty";
    } elseif ($puluh == 3) {
    $words .= " thirty";
    } elseif ($puluh == 5) {
    $words .= " fifty";
    } elseif ($puluh == 5) {
    $words .= " eighty";
    } else {
    $words .= " ".$this->numb[$puluh]."ty";
    }
    $sisa = $this->mod($sisa,10);
    if ($sisa>0&&$sisa<=9) {
    $words .= " ".$this->numb[$sisa];
    }
    if ($amount>0&&$amount<=1000) {
    $words .= " ".$suffix;
    }
    return $words;
    }
    }
    // END CONVERTION CLASS
    // THIS LINE BELOW SHOW YOU HOW TO RUN IT
    $int = $_GET["int"];
    if (!isset($int)) $int = "0";
    if ($int>999999999999999.99||$int<-999999999999999.99) {
    print "Out of value";
    } else {
    $cc = new num2words;
    $cc->setTail("dollars");
    $cc->setNumber($int);
    $cc->printCurrency();
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

    Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
    FREE! Go There Now!


    NEW! IBM Rational Systems Development e-Kit

    As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br />
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Krugle, developerWorks, and code search

    Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users.
    FREE! Go There Now!


    NEW! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Performance Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Connectivity

    Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek