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
developerWorks - FREE Tools! |
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |