Re: [PHP] number to word converter

2001-08-31 Thread Hugh Bothwell

Here is a slightly more readable number-to-English converter.  It will deal
with negative numbers and recognizes zero; it's table-driven, so it
shouldn't be too hard to convert to other languages.

It will convert anything in +/- 10^30.  This should be pretty comprehensive;
if not, extend the triplets table (and email me to explain what on Earth
you're doing with it!! ;-)

 0)
   $str = $ones[$x] . " hundred";

  // do ones and tens
  if ($y < 20)
   $str .= $ones[$y];
  else
   $str .= $tens[(int) ($y / 10)] . $ones[$y % 10];

  // add triplet modifier only if there
  // is some output to be modified...
  if ($str != "")
   $str .= $triplets[$tri];

  // continue recursing?
  if ($r > 0)
   return convertTri($r, $tri+1).$str;
  else
   return $str;
 }

// returns the number as an anglicized string
function convertNum($num) {
 $num = (int) $num;// make sure it's an integer

 if ($num < 0)
  return "negative".convertTri(-$num, 0);

 if ($num == 0)
  return "zero";

 return convertTri($num, 0);
}

?>

and a test fn I wrote,

$num: ".convertNum($num);
}
?>

Usage is simple:
convertNum(integer) returns a string, the value in English.

Hope this helps.





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] number to word converter

2001-08-31 Thread


From: * R&zE: <[EMAIL PROTECTED]>
Date: Fri, Aug 31, 2001 at 02:03:06PM +0200
Message-ID: <[EMAIL PROTECTED]>
Subject: Re: [PHP] number to word converter

Oh... btw... It works uptil 99. So you can convert everything
between 1 and 99. Zero (0) cannot be converted (stupid but true,
and easy to patch in) and everything >= a million will result in
very weird results.

But like I said... it's just a quickly written function and the most
used numbers you can convert.

-- 

* R&zE:


-- 
-- Renze Munnik
-- DataLink BV
--
-- E: [EMAIL PROTECTED]
-- W: +31 23 5326162
-- F: +31 23 5322144
-- M: +31 6 21811143
--
-- Stationsplein 82
-- 2011 LM  HAARLEM
-- Netherlands
--
-- http://www.datalink.nl
-- 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] number to word converter

2001-08-31 Thread


From: Carry Ian <[EMAIL PROTECTED]>
Date: Fri, Aug 31, 2001 at 12:36:20PM +0200
Message-ID: <[EMAIL PROTECTED]>
Subject: [PHP] number to word converter

> Hello,
> 
> can anybody suggest me where could i get a script to convert a integer value to a 
>phrase.
> for eg. "135" to "One Hundred And Thirty Five"
> 
> thanks in advance.
> 
> carry





In some spare time I just created this function. It's prob. far from
optimal, so maybe you'll have to give a look in order to optimize
it. But at least it works.

--- PHP code ---
 2) {
  if ($togo > 3) {
$returnString .= inttoword (substr($x, 0, $togo-3));
$returnString .= " Thousand";
$pos += ($togo-4);
  } else {
if ((integer)substr($x, $pos, 1) > 0) {
  $returnString .= $digit[((integer)substr($x, $pos, 1))-1];
  $returnString .= " Hundred";
  $hundred = true;
}
  }
} elseif ($togo == 2) {
  if ((integer)substr($x, $pos, 1) == 1) {
if ($hundred) $returnString .= "and ";
$returnString .= $digit[((integer)substr($x, $pos, 2))-1];
break;
  } else {
if ((integer)substr($x, $pos, 1) > 0) {
  if ($hundred) $returnString .= "and ";
  $hundred = false;
  $returnString .= $tens[((integer)substr($x, $pos, 1))-2];
}
$pos++;
$returnString .= " ";
if ((integer)substr($x, $pos, 1)) {
  if ($hundred) $returnString .= "and ";
  $returnString .= $digit[((integer)substr($x, $pos, 1))-1];
}
break;
  }
} else {
  if ($hundred) $returnString .= "and ";
  $returnString .= $digit[((integer)substr($x, $pos, 1))-1];
}
  }

  return (preg_replace ("/\s+/", " ",
  preg_replace ("/^\s+/", "", $returnString)));
}

?>




--- End of PHP code ---


Let me know if this is what you're looking for and/or if you have
optimized it.

Good luck!



-- 

* R&zE:


-- 
-- Renze Munnik
-- DataLink BV
--
-- E: [EMAIL PROTECTED]
-- W: +31 23 5326162
-- F: +31 23 5322144
-- M: +31 6 21811143
--
-- Stationsplein 82
-- 2011 LM  HAARLEM
-- Netherlands
--
-- http://www.datalink.nl
-- 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] number to word converter

2001-08-31 Thread Carry Ian

Hello,

can anybody suggest me where could i get a script to convert a integer value to a 
phrase.
for eg. "135" to "One Hundred And Thirty Five"

thanks in advance.

carry



--
Get real solutions to all your problems.

http://www.salahkarindia.com - India's first advisory Portal

Your friend, advisor, healer,companion!!!

Register now  to  get free advice on all your problems.

--


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]