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! |
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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!
|
|
|
|
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! |