CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Number to letters

Print topic Send  topic

Author Message
Jose Maldonado
Posted: 04/30/2005, 5:58 AM

This is a multi-part message in MIME format.

------=_NextPart_000_0071_01C54D5F.AD5221C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

There is an example in english, but I looking for spanish example =
(Numeros a letras), if someone know the method or funcion to do I will =
preciate.

Thanks.

Jose
------=_NextPart_000_0071_01C54D5F.AD5221C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2627" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>There is an example in english, but I =
looking for=20
spanish example (Numeros a letras), if someone know the method or =
funcion to do=20
I will preciate.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Jose</FONT></DIV></BODY></HTML>

------=_NextPart_000_0071_01C54D5F.AD5221C0--
Fernando Colin
Posted: 04/30/2005, 8:50 AM

Hello, perhaps this would help.


<?php
#
# Number to String
#
#-----------------------------------------------
# Examples
#
# Use : number2string(169)
# Return: ciento sesenta y nueve
#
# Use : number2string(35.652)
# Return: treinta y cinco con seis cientos cincuenta y dos
#
#

function tercia($num)
{
$unidades=array(0=>'cero',1=>'uno',2=>'dos',3=>'tres',4=>'cuatro',5=>'cinco',6=>'seis',7=>'siete',8=>'ocho',9=>'nueve');
$teens=array(0=>'diez',1=>'once',2=>'doce',3=>'trece',4=>'catorce',6=>'quince');
$tens=array(1=>'diez',2=>'veinte',3=>'treinta',4=>'cuarenta',5=>'cincuenta',6=>'sesenta',7=>'setenta',8=>'ochenta',9=>'noventa');


$numero=''.$num;
if (strlen($numero) == 1)
$numero='00'.$numero;
if (strlen($numero) == 2)
$numero='0'.$numero;
$a=$numero[0];
$b=$numero[1];
$c=$numero[2];

//PARA DEBUG
// echo "A: $a<br>";
// echo "B: $b<br>";
// echo "C: $c<br>";

if($a == 0)
{
if($b == 0)
{

$resultado=$unidades[$c];
return $resultado;
}
else if($b == 1)
{
if($c >= 0 && $c <= 5)
{
$resultado = $teens[$c];
return $resultado;
}
else if($c >= 6 and $c <= 9)
{
$resultado = $tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
else if($b == 2)
{
if($c==0)
{
$resultado = 'veinte';
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado='veinti'.$unidades[$c];
return $resultado;
}
}
else if($b >=3 and $b <= 9)
{
if ($c == 0)
{
$resultado = $tens[$b];
return $resultado;
}
if ($c >= 1 and $c <= 9)
{
$resultado = $tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
}
if ($a == 1)
{
if($b == 0)
{
if($c == 0)
{
$resultado = 'cien';
return $resultado;
}
else if( $c > 0 and $c <= 9)
{
$resultado ='ciento '.$unidades[$c];
return $resultado;
}
}
else if( $b == 1)
{
if ($c >= 0 and $c <= 5)
{
$resultado = 'ciento '.$teens[$c];
return $resultado;
}
else if($c >= 6 and $c <= 9)
{
$resultado = 'ciento '.$tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
else if($b == 2)
{
if($c == 0)
{
$resultado = 'ciento veinte';
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado ='ciento veinti'.$unidades[$c];
return $resultado;
}
}
else if($b >= 3 and $b <= 9)
{
if($c == 0)
{
$resultado = 'ciento '.$tens[$b];
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado='ciento '.$tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
}
else if($a >= 2 and $a <= 9)
{
if ($a == 5)
{ $prefix='quinientos '; }
else if($a == 7)
{ $prefix='setecientos '; }
else if($a == 9)
{ $prefix='novecientos '; }
else
{ $prefix=$unidades[$a].' cientos '; }

if($b == 0)
{
if($c == 0)
{
$resultado = $prefix;
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado = $prefix.' '.$unidades[$c];
return $resultado;
}
}
else if($b == 1)
{
if ( $c >= 0 and $c <= 5 )
{
$resultado = $prefix.''.$teens[$c];
return $resultado;
}
else if($c >= 6 and $c <= 9)
{
$resultado = $prefix.' '.$tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
else if($b == 2)
{
if($c == 0)
{
$resultado = $prefix.' veinte';
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado = $prefix.' veinti'.$unidades[$c];
return $resultado;
}
}
else if($b >= 3 and $b <= 9)
{
if($c == 0)
{
$resultado = $prefix.''.$tens[$b];
return $resultado;
}
else if($c > 0 and $c <= 9)
{
$resultado = $prefix.''.$tens[$b].' y '.$unidades[$c];
return $resultado;
}
}
}
}

function principal($num)
{
$result='';
$numero=''.$num;

if(strlen($numero) == 1)
$numero='00000000'.$numero;
if(strlen($numero) == 2)
$numero='0000000'.$numero;
if(strlen($numero) == 3)
$numero='000000'.$numero;
if(strlen($numero) == 4)
$numero='00000'.$numero;
if(strlen($numero) == 5)
$numero='0000'.$numero;
if(strlen($numero) == 6)
$numero='000'.$numero;
if(strlen($numero) == 7)
$numero='00'.$numero;
if(strlen($numero) == 8)
$numero='0'.$numero;
$posicion=1;

for($j=0;$j<3;$j++)
{
$i=$j*3;
$valor=$numero[$i].$numero[$i+1].$numero[$i+2];
if ( $valor != 0 )
{
$res=tercia($valor);
if($i == 0)
{
$result=$res." millones ";
}
else if($i == 3)
{
$result=$result.$res." mil ";
}
else if($i == 6)
{
$result=$result.''.$res;
}
}
}
return $result;
}
function number2string($number)
{
if(! is_numeric($number) )
return "Numero invalido";
$number=''.$number;
list($entero,$fraccion)=split('\.',$number);
if($fraccion>0)
$result=principal($entero).' con '.principal($fraccion);
else
$result=principal($entero);

return $result;
}

?>
Jose Maldonado
Posted: 04/30/2005, 10:42 AM

Where is the most convenient place tu put this funcion to be available to
the entire aplication?

Thanks.

Jose
peterr


Posts: 5971
Posted: 04/30/2005, 11:31 AM

At the end of Common.php.
Or create a seperate file and include it at the beginning of Common.php.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
Jose Maldonado
Posted: 04/30/2005, 12:52 PM

deluxe help. One last question.

When I include on Common.php all this functions, how can I call those
functions on desing view?

"peterr" <peterr@forum.codecharge> escreveu na mensagem
news:54273ceee07521@news.codecharge.com...
> At the end of Common.php.
> Or create a seperate file and include it at the beginning of Common.php.
> _________________
> Peter R.
> YesSoftware Forums Moderator
> For product support please visit http://support.yessoftware.com
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

Fernando Colin
Posted: 04/30/2005, 1:27 PM

here is an example in the before show event whit a label call label1 that exist inside a form call form1.

form1->label1->SetValue(number2string(1546));


Lwismanuel
Posted: 04/30/2005, 6:05 PM

Hi Jose,

I remember seeing a good class on phpclass.org that does exactly that. Here it goes:

<?php
/**
* OEOG Class para convertir numeros en palabras
*
*
* @version $Id: CNumeroaLetra.php,v 1.0.1 2004-10-29 13:20 ortizom Exp $
* @author Omar Eduardo Ortiz Garza <ortizom@siicsa.com>
* @copyright (c) 2004-2005 Omar Eduardo Ortiz Garza
* @since Friday, October 29, 2004
**/
/***************************************************************************
*
* Este programa es software libre; puedes redistribuir y/o modificar
* bajo los terminos de la GNU General Public License como se publico por
* la Free Software Foundation; version 2 de la Licencia, o cualquier
* (a tu eleccion) version posterior.
*
***************************************************************************/

/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/

class CNumeroaLetra{
/***************************************************************************
*
* Propiedades:
* $numero: Es la cantidad a ser convertida a letras maximo 999,999,999,999.99
* $genero: 0 para femenino y 1 para masculino, es util dependiendo de la
* moneda ej: cuatrocientos pesos / cuatrocientas pesetas
* $moneda: nombre de la moneda
* $prefijo: texto a imprimir antes de la cantidad
* $sufijo: texto a imprimir despues de la cantidad
* tanto el $sufijo como el $prefijo en la impresion de cheques o
* facturas, para impedir que se altere la cantidad
* $mayusculas: 0 para minusculas, 1 para mayusculas indica como debe
* mostrarse el texto
* $textos_posibles: contiene todas las posibles palabras a usar
* $aTexto: es el arreglo de los textos que se usan de acuerdo al genero
* seleccionado
*
***************************************************************************/

private $numero=0;
private $genero=1;
private $moneda="PESOS";
private $prefijo="(***";
private $sufijo="***)";
private $mayusculas=1;
//textos
private $textos_posibles= array(
0 => array ('UNA ','DOS ','TRES ','CUATRO ','CINCO ','SEIS ','SIETE ','OCHO ','NUEVE ','UN '),
1 => array ('ONCE ','DOCE ','TRECE ','CATORCE ','QUINCE ','DIECISEIS ','DIECISIETE ','DIECIOCHO ','DIECINUEVE ',''),
2 => array ('DIEZ ','VEINTE ','TREINTA ','CUARENTA ','CINCUENTA ','SESENTA ','SETENTA ','OCHENTA ','NOVENTA ','VEINTI'),
3 => array ('CIEN ','DOSCIENTAS ','TRESCIENTAS ','CUATROCIENTAS ','QUINIENTAS ','SEISCIENTAS ','SETECIENTAS ','OCHOCIENTAS ','NOVECIENTAS ','CIENTO '),
4 => array ('CIEN ','DOSCIENTOS ','TRESCIENTOS ','CUATROCIENTOS ','QUINIENTOS ','SEISCIENTOS ','SETECIENTOS ','OCHOCIENTOS ','NOVECIENTOS ','CIENTO '),
5 => array ('MIL ','MILLON ','MILLONES ','CERO ','Y ','UNO ','DOS ','CON ','','')
);
private $aTexto;

/***************************************************************************
*
* Metodos:
* _construct: Inicializa textos
* setNumero: Asigna el numero a convertir a letra
* setPrefijo: Asigna el prefijo
* setSufijo: Asiga el sufijo
* setMoneda: Asigna la moneda
* setGenero: Asigan genero
* setMayusculas: Asigna uso de mayusculas o minusculas
* letra: Convierte numero en letra
* letraUnidad: Convierte unidad en letra, asigna miles y millones
* letraDecena: Contiene decena en letra
* letraCentena: Convierte centena en letra
*
***************************************************************************/
function __construct(){
for($i=0; $i<6;$i++)
for($j=0;$j<10;$j++)
$this->aTexto[$i][$j]=$this->textos_posibles[$i][$j];
}

function setNumero($num){
$this->numero=(double)$num;
}

function setPrefijo($pre){
$this->prefijo=$pre;
}

function setSufijo($sub){
$this->sufijo=$sub;
}

function setMoneda($mon){
$this->moneda=$mon;
}

function setGenero($gen){
$this->genero=(int)$gen;
}

function setMayusculas($may){
$this->mayusculas=(int)$may;
}

function letra(){
if($this->genero==1){ //masculino
$this->aTexto[0][0]=$this->textos_posibles[5][5];
for($j=0;$j<9;$j++)
$this->aTexto[3][$j]= $this->aTexto[4][$j];

}else{//femenino
$this->aTexto[0][0]=$this->textos_posibles[0][0];
for($j=0;$j<9;$j++)
$this->aTexto[3][$j]= $this->aTexto[3][$j];
}

$cnumero=sprintf("%015.2f",$this->numero);
$texto="";
if(strlen($cnumero)>15){
$texto="Excede tamaño permitido";
}else{
$hay_significativo=false;
for ($pos=0; $pos<12; $pos++){
// Control existencia Dígito significativo
if (!($hay_significativo)&&(substr($cnumero,$pos,1) == '0')) ;
else $hay_dignificativo = true;

// Detectar Tipo de Dígito
switch($pos % 3) {
case 0: $texto.=$this->letraCentena($pos,$cnumero); break;
case 1: $texto.=$this->letraDecena($pos,$cnumero); break;
case 2: $texto.=$this->letraUnidad($pos,$cnumero); break;
}
}
// Detectar caso 0
if ($texto == '') $texto = $this->aTexto[5][3];
if($this->mayusculas){//mayusculas
$texto=strtoupper($this->prefijo.$texto." ".$this->moneda." ".substr($cnumero,-2)."/100 ".$this->sufijo);
}else{//minusculas
$texto=strtolower($this->prefijo.$texto." ".$this->moneda." ".substr($cnumero,-2)."/100 ".$this->sufijo);
}
}
return $texto;

}

public function __toString() {
return $this->letra();
}

//traducir letra a unidad
private function letraUnidad($pos,$cnumero){
$unidad_texto="";
if( !((substr($cnumero,$pos,1) == '0') ||
(substr($cnumero,$pos - 1,1) == '1') ||
((substr($cnumero, $pos - 2, 3) == '001') && (($pos == 2) || ($pos == 8)) )
)
){
if((substr($cnumero,$pos,1) == '1') && ($pos <= 6)){
$unidad_texto.=$this->aTexto[0][9];
}else{
$unidad_texto.=$this->aTexto[0][substr($cnumero,$pos,1) - 1];
}
}
if((($pos == 2) || ($pos == 8)) &&
(substr($cnumero, $pos - 2, 3) != '000')){//miles
if(substr($cnumero,$pos,1)=='1'){
$unidad_texto=substr($unidad_texto,0,-2)." ";
$unidad_texto.= $this->aTexto[5][0];
}else{
$unidad_texto.=$this->aTexto[5][0];
}
}
if($pos == 5 && substr($cnumero, $pos - 2, 3) != '000'){
if(substr($cnumero, 1, 6) == '000001'){//millones
$unidad_texto.=$this->aTexto[5][1];
}else{
$unidad_texto.=$this->aTexto[5][2];
}
}
return $unidad_texto;
}
//traducir digito a decena
private function letraDecena($pos,$cnumero){
$decena_texto="";
if (substr($cnumero,$pos,1) == '0'){
return;
}else if(substr($cnumero,$pos + 1,1) == '0'){
$decena_texto.=$this->aTexto[2][substr($cnumero,$pos,1)-1];
}else if(substr($cnumero,$pos,1) == '1'){
$decena_texto.=$this->aTexto[1][substr($cnumero,$pos+ 1,1)- 1];
}else if(substr($cnumero,$pos,1) == '2'){
$decena_texto.=$this->aTexto[2][9];
}else{
$decena_texto.=$this->aTexto[2][substr($cnumero,$pos,1)- 1] . $this->aTexto[5][4];
}
return $decena_texto;
}
//traducir digito centena
private function letraCentena($pos,$cnumero){
$centena_texto="";
if (substr($cnumero,$pos,1) == '0') return;
$pos2 = 3;
if((substr($cnumero,$pos,1) == '1') && (substr($cnumero,$pos+ 1, 2) != '00')){
$centena_texto.=$this->aTexto[$pos2][9];
}else{
$centena_texto.=$this->aTexto[$pos2][substr($cnumero,$pos,1) - 1];
}
return $centena_texto;
}

}

?>

The following is an example on how to use it.

<?php
/**
* OEOG Ejemplo de uso de Class para convertir numeros en palabras
* Probado en/Tested on PHP 5-Apache2-XP
*
*
* @version $Id: CNumeroaLetra_ejemplo.php,v 1.0.0 2004-10-29 13:20 ortizom Exp $
* @author Omar Eduardo Ortiz Garza <ortizom@siicsa.com>
* @copyright (c) 2004-2005 Omar Eduardo Ortiz Garza
* @since Friday, October 29, 2004
**/

include("CNumeroaLetra.php");
$numalet= new CNumeroaletra;
$numalet->setNumero(987654321098.76);
//imprime numero con los valore por defecto
echo $numalet->letra();
?>
<br>
<?
//cambia a minusculas
$numalet->setMayusculas(0);
//cambia a femenino
$numalet->setGenero(0);
//cambia moneda
$numalet->setMoneda("Pesetas");
//cambia prefijo
$numalet->setPrefijo("--");
//cambia sufijo
$numalet->setSufijo("++");
//imprime numero con los cambios
echo $numalet->letra();

?>

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.