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!


    Build Forge Express demo: Enabling software delivery excellence for small and midsized businesses

    This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process.
    FREE! Go There Now!


    NEW! Cook up Web sites fast with CakePHP, Part 4: Use CakePHP&apos;s Session and Request Handler components

    CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    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! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Successful Change and Release Management for .NET

    Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months.
    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 Manual Tester V7.0.1

    Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually.
    FREE! Go There Now!


    NEW! Try IBM Rational Asset Manager V7.0 online!

    You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for People

    Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - 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
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





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