<?php
/***********************************************************
* Class: num2words (adapted form baa2words)
* Date: June 2006
* Author: Luis Pinto
* Original:
* Version: 1.0
* Date: June 2003
* Author: Barry A Andrew
* Copyright: © BA Andrew 2003
***********************************************************
USAGE SAMPLE
include('num2words.php');
$amount = 12345.67
$obj = new num2words( $amount )
echo $obj->words; // gives doze mil e trezentos e quarenta cinco Euros e sessenta sete centimos
echo $obj->number; // gives 12,345.67
$obj = new num2words( $amount , 'dollars', 'c')
echo $obj->words; // gives doze mil e trezentos e quarenta cinco dollars e sessenta sete c
echo $obj->number; // gives 12,345.67
To alter default currency units, change the defines below
**************************************************************/
define("MAJOR", 'Euros');
define("MINOR", 'centimos');
class num2words {
var $pounds;
var $pence;
var $major;
var $minor;
var $words = '';
var $number;
var $magind;
var $units = array('','um','dois','tres','quatro','cinco','seis','sete','oito','nove');
var $teens = array('dez','onze','doze','treze','quatorze','quinze','dezassies','dezassete','dezoito','dezanove');
var $tens = array('','dez','vinte','trinta','quarenta','cinquenta','sessenta','setenta','oitenta','noventa');
var $cens = array('','cento','duzentos','trezentos','quatrocentos','quinhentos','seiscentos','setecentos','oitocentos','novecentos');
var $mag = array('','mil','milhão','bilhão','trilhão');
function num2words($amount, $major=MAJOR, $minor=MINOR) {
$this->major = $major;
$this->minor = $minor;
$this->number = number_format($amount,2);
list($this->pounds,$this->pence) = explode('.',$this->number);
if ($this->pence==0){
$this->words = " $this->major";
}else{
$groups = explode(',',$this->pence);
$groups = array_reverse($groups);
$this->words = " $this->major e $this->pence $this->minor";
for ($this->magind=0; $this->magind<count($groups); $this->magind++) {
if (($this->magind==1)&&(strpos($this->words,'cem') === false)&&($groups[0]!='000'))
$this->words = ' e ' . $this->words;
$this->words =$this->major . ' e ' . $this->_build($groups[$this->magind]).$this->minor;
}
}
if ($this->pounds==0)
$this->words = "zero $this->words";
else {
$groups = explode(',',$this->pounds);
$groups = array_reverse($groups);
for ($this->magind=0; $this->magind<count($groups); $this->magind++) {
if (($this->magind==1)&&(strpos($this->words,'cem') === false)&&($groups[0]!='000'))
$this->words = ' e ' . $this->words;
$this->words = $this->_build($groups[$this->magind]).$this->words;
}
}
}
function _build($n) {
$res = '';
$na = str_pad("$n",3,"0",STR_PAD_LEFT);
if ($na == '000') return '';
if ($na == '100') return ' cem ';
if ($na{0} != 0 && $na != '100')
$res = ' '.$this->cens[$na{0}];
if (($na{1}=='0')&&($na{2}=='0'))
return $res . ' ' . $this->mag[$this->magind];
$res .= $res==''? '' : ' e';
$t = (int)$na{1}; $u = (int)$na{2};
switch ($t) {
case 0: $res .= ' ' . $this->units[$u]; break;
case 1: $res .= ' ' . $this->teens[$u]; break;
default:$res .= ' ' . $this->tens[$t] . ' ' . $this->units[$u] ; break;
}
$res .= ' ' . $this->mag[$this->magind];
return $res;
}
}
?>