This topic is locked
[SOLVED]

  Number to text

8/14/2011 7:03:15 PM
PHPRunner General questions
A
aamato author

Hi,
Is it possible to automatically convert number to text in Editor ??

Does it need a php snippet ? Is it possible in other languages ? I need in portuguese
Thanks a lot

C
cgphp 8/14/2011
A
aamato author 8/14/2011



Something like this ?
http://www.phpro.org/examples/Convert-Numbers-to-Words.html


Hi Cristian, thanks again.
I am using the snippet below inside Custom code in Visual Editor, but I can't make it work, I get Fatal error: Call to undefined function convert_number() in C:\..\..\..\PHPRunnerProjects\Project1\output\include\phpfunctions.php on line 535



$value = convert_number($data);
function convert_number($number)

{

if (($number < 0) || ($number > 999999999))


$Gn = floor($number / 1000000); /* Millions (giga) */

$number -= $Gn * 1000000;

$kn = floor($number / 1000); /* Thousands (kilo) */

$number -= $kn * 1000;

$Hn = floor($number / 100); /* Hundreds (hecto) */

$number -= $Hn * 100;

$Dn = floor($number / 10); /* Tens (deca) */

$n = $number % 10; /* Ones */
$res = "";
if ($Gn)

{

$res .= convert_number($Gn) . " Million";

}
if ($kn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($kn) . " Thousand";

}
if ($Hn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($Hn) . " Hundred";

}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",

"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",

"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",

"Nineteen");

$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

"Seventy", "Eigthy", "Ninety");
if ($Dn || $n)

{

if (!empty($res))

{

$res .= " and ";

}
if ($Dn < 2)

{

$res .= $ones[$Dn * 10 + $n];

}

else

{

$res .= $tens[$Dn];
if ($n)

{

$res .= "-" . $ones[$n];

}

}

}
if (empty($res))

{

$res = "zero";

}
return $res;

}
C
cgphp 8/14/2011

Place the $value assignment after the function:



function convert_number($number)

{

if (($number < 0) || ($number > 999999999))

{

$value ="Number out of range";

}
$Gn = floor($number / 1000000); /* Millions (giga) */

$number -= $Gn * 1000000;

$kn = floor($number / 1000); /* Thousands (kilo) */

$number -= $kn * 1000;

$Hn = floor($number / 100); /* Hundreds (hecto) */

$number -= $Hn * 100;

$Dn = floor($number / 10); /* Tens (deca) */

$n = $number % 10; /* Ones */
$res = "";
if ($Gn)

{

$res .= convert_number($Gn) . " Million";

}
if ($kn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($kn) . " Thousand";

}
if ($Hn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($Hn) . " Hundred";

}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",

"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",

"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",

"Nineteen");

$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

"Seventy", "Eigthy", "Ninety");
if ($Dn || $n)

{

if (!empty($res))

{

$res .= " and ";

}
if ($Dn < 2)

{

$res .= $ones[$Dn * 10 + $n];

}

else

{

$res .= $tens[$Dn];
if ($n)

{

$res .= "-" . $ones[$n];

}

}

}
if (empty($res))

{

$res = "zero";

}
return $res;

}

$value = convert_number($value);


But, this is not a very elegant solution. A best approach is to load the convert_number() function as an external php file, so you can reuse it across your application (http://xlinesoft.com/phprunner/docs/how_to_add_external_files.htm)

A
aamato author 8/18/2011



Place the $value assignment after the function:



function convert_number($number)

{

if (($number < 0) || ($number > 999999999))

{

$value ="Number out of range";

}
$Gn = floor($number / 1000000); /* Millions (giga) */

$number -= $Gn * 1000000;

$kn = floor($number / 1000); /* Thousands (kilo) */

$number -= $kn * 1000;

$Hn = floor($number / 100); /* Hundreds (hecto) */

$number -= $Hn * 100;

$Dn = floor($number / 10); /* Tens (deca) */

$n = $number % 10; /* Ones */
$res = "";
if ($Gn)

{

$res .= convert_number($Gn) . " Million";

}
if ($kn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($kn) . " Thousand";

}
if ($Hn)

{

$res .= (empty($res) ? "" : " ") .

convert_number($Hn) . " Hundred";

}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",

"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",

"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",

"Nineteen");

$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

"Seventy", "Eigthy", "Ninety");
if ($Dn || $n)

{

if (!empty($res))

{

$res .= " and ";

}
if ($Dn < 2)

{

$res .= $ones[$Dn * 10 + $n];

}

else

{

$res .= $tens[$Dn];
if ($n)

{

$res .= "-" . $ones[$n];

}

}

}
if (empty($res))

{

$res = "zero";

}
return $res;

}

$value = convert_number($value);


But, this is not a very elegant solution. A best approach is to load the convert_number() function as an external php file, so you can reuse it across your application (http://xlinesoft.com/phprunner/docs/how_to_add_external_files.htm)


Thanks, it worked
Alexandre Amato

www.amato.com.br

A
aamato author 8/20/2011



Thanks, it worked
Alexandre Amato

www.amato.com.br


This is the code I used for portuguese

function extenso($valor, $maiusculas=false)

{

// verifica se tem virgula decimal

if (strpos($valor,",") > 0)

{

// retira o ponto de milhar, se tiver

$valor = str_replace(".","",$valor);
// troca a virgula decimal por ponto decimal

$valor = str_replace(",",".",$valor);

}

$rt = "";

$singular = array("centavo", "real", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");

$plural = array("centavos", "reais", "mil", "milhões", "bilhões", "trilhões",

"quatrilhões");
$c = array("", "cem", "duzentos", "trezentos", "quatrocentos",

"quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos");

$d = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta",

"sessenta", "setenta", "oitenta", "noventa");

$d10 = array("dez", "onze", "doze", "treze", "quatorze", "quinze",

"dezesseis", "dezesete", "dezoito", "dezenove");

$u = array("", "um", "dois", "três", "quatro", "cinco", "seis",

"sete", "oito", "nove");
$z=0;
$valor = number_format($valor, 2, ".", ".");

$inteiro = explode(".", $valor);

$cont=count($inteiro);

for($i=0;$i<$cont;$i++)

for($ii=strlen($inteiro[$i]);$ii<3;$ii++)

$inteiro[$i] = "0".$inteiro[$i];
$fim = $cont - ($inteiro[$cont-1] > 0 ? 1 : 2);

for ($i=0;$i<$cont;$i++) {

$valor = $inteiro[$i];

$rc = (($valor > 100) && ($valor < 200)) ? "cento" : $c[$valor[0]];

$rd = ($valor[1] < 2) ? "" : $d[$valor[1]];

$ru = ($valor > 0) ? (($valor[1] == 1) ? $d10[$valor[2]] : $u[$valor[2]]) : "";
$r = $rc.(($rc && ($rd || $ru)) ? " e " : "").$rd.(($rd &&

$ru) ? " e " : "").$ru;

$t = $cont-1-$i;

$r .= $r ? " ".($valor > 1 ? $plural[$t] : $singular[$t]) : "";

if ($valor == "000")$z++; elseif ($z > 0) $z--;

if (($t==1) && ($z>0) && ($inteiro[0] > 0)) $r .= (($z>1) ? " de " : "").$plural[$t];

if ($r) $rt = $rt . ((($i > 0) && ($i <= $fim) &&

($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? ", " : " e ") : " ") . $r;

}
if(!$maiusculas)

{

return($rt ? $rt : "zero");

} elseif($maiusculas == "2") {

return (strtoupper($rt) ? strtoupper($rt) : "Zero");

} else {

return (ucwords($rt) ? ucwords($rt) : "Zero");

}
}

$value = extenso($value, false);