Forum Moderators: coopster

Message Too Old, No Replies

Number into Words?

How to write a number in words like when you write a check?

         

ngentot

2:14 am on Aug 3, 2004 (gmt 0)



Hello,

Is there any PHP script out there that lets you print a string that represents a number?

For example:

100 will print "One Hundred"
324 will print "Three Hundred Twenty Four"
2317 will print "Two Thousand Three Hundred Seventeen"
and so on.

It's like when you write a check.

Thanks.

WhosAWhata

3:08 am on Aug 3, 2004 (gmt 0)

10+ Year Member



i can't remember where exactly but i saw this either in the PHP library or a link from it

WhosAWhata

3:25 am on Aug 3, 2004 (gmt 0)

10+ Year Member



Originally posted by DrDoc [[webmasterworld.com ]]
21 -> Twenty-first
This is a tweaked and improved version of the "numerstowords" class.


Changes:
• The original class contained a couple of spelling errors ("fourty" and "eightteen")
• It also added "and" way too often. ("two thousand and three hundred and twelve" instead of "two thousand three hundred and twelve")
• The class didn't hyphenate certain numbers. ("twenty one" instead of "twenty-one")
• I've added two options: (1) You can choose whether you want ordinal suffixes added ("one" vs. "first") (2) You can choose whether you want the string capitalized ("two thousand and three" vs. "Two Thousand and Three")


spelloutnumbers.php:
<?php
class spelloutnumbers {

function hundreds($number) {
$lasts = array('one','two','three','four','five','six','seven','eight','nine');
$teens = array('eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
$tens = array('ten','twenty-','thirty-','forty-','fifty-','sixty-','seventy-','eighty-','ninety-');
$many = array('', ' thousand ',' million ',' billion ',' trillion ');

$string = "";
$j = strlen($number);
$done = false;
for($i=0; $i<strlen($number); $i++) {
if($j==2) {
if(strlen($number)>2) {
if($number[0]!=0) {$string .= ' hundred ';}
}
if($number[$i]==1) {
if($number[$i+1]==0) {$string .= $tens[$number[$i]-1];}
else {
$string .= $teens[$number[$i+1]-1];
$done = true;
}
}
else {
if(!empty($tens[$number[$i]-1])) {$string .= $tens[$number[$i]-1].' ';}
}
}
elseif($number[$i]!=0 &&!$done) {$string .= $lasts[$number[$i]-1];}
$j--;
}
return $string;
}

function spellout($number,$suffix=0,$caps=1,$uk=0) {
$lasts = array('one','two','three','four','five','six','seven','eight','nine');
$teens = array('eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
$tens = array('ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');
$many = array('', ' thousand ',' million ',' billion ',' trillion ');

$foo = strval($number);
$string = "";

if(strlen($foo)%3!=0) {
$string .= $this->hundreds(substr($number,0,strlen($number)%3));
$string .= $many[floor(strlen($number)/3)];
}

for($i=0; $i<floor(strlen($number)/3); $i++) {
$string .= $this->hundreds(substr($number,strlen($number)%3+($i*3),3));
if($number[strlen($number)%3+($i*3)]!=0) {
$string .= $many[floor(strlen($number)/3)-1-$i];
}
}

$match = array('/ +/','/- /','/-$/','/-thousand/','/-million/','/-billion/','/-trillion/');
$replace = array(' ','-','',' thousand',' million',' billion',' trillion');
$string = preg_replace($match,$replace,$string);

if($uk) {
$match = array('/billion/','/trillion/');
$replace = array('milliard','billion');
$string = preg_replace($match,$replace,$string);
}

if($suffix) {
$match = array('/ve$/','/t$/','/y$/','/one$/','/two$/','/three$/','/e$/');
$replace = array('fth','th','ieth','first','second','third','th');
$string = preg_replace($match,$replace,$string);
if(!preg_match("/first$¦second$¦third$¦th$/",$string)) {
$string .= "th";
}
}

if($caps) {
$string = ucwords($string);
$string = preg_replace("/(-[A-Z])/",strtolower("\\1"),$string);
}

$foo = strrpos($string," ");
if($foo!==false) {
$string = substr($string,0,$foo)." and".substr($string,$foo);
}

return $string;
}
}
?>



Syntax:
include("spelloutnumbers.php");
$BLAH = new spelloutnumbers();
$myvar = $BLAH->spellout(number[,suffix[,caps[,uk]]]);

number is the number you want spelled out.
It can be an integer or a string. Range = 1 to 999.999.999.999.999

suffix can be set to 0 or 1.
1 = use ordinal suffix ("first" instead of "one" etc.). Default is 0.

caps can be set to 0 or 1.
1 = capitalize the words ("Two Thousand" instead of "two thousand" etc.). Default is 1.

uk can be set to 0 or 1.
1 = use British standard ("milliard" instead of "billion" etc.). Default is 0.



Examples:
spellout(3) prints Three
spellout(3,1) prints Third
spellout(3,1,0) prints third
spellout(2003) prints Two Thousand and Three
spellout(2003,1) prints Two Thousand and Third
spellout(999999999999999) prints Nine Hundred Ninety-nine Trillion Nine Hundred Ninety-nine Billion Nine Hundred Ninety-nine Million Nine Hundred Ninety-nine Thousand Nine Hundred and Ninety-nine
spellout(999999999999999,0,1,1) prints Nine Hundred Ninety-nine Billion Nine Hundred Ninety-nine Milliard Nine Hundred Ninety-nine Million Nine Hundred Ninety-nine Thousand Nine Hundred and Ninety-nine
spellout(990) prints Nine Hundred and Ninety
spellout(990,1) prints Nine Hundred and Ninetieth
spellout(990,1,0) prints nine hundred and ninetieth

ngentot

5:03 am on Aug 3, 2004 (gmt 0)



Hey this is a great script. Thanks.

But I notice that if you have, say, 24, it prints "Twenty-four". How can I get "four" to be capitalized ("Four")?

Thanks.

WhosAWhata

5:37 pm on Aug 3, 2004 (gmt 0)

10+ Year Member



ucwords()