0
Good,
I have the numbers for example:
10
1.000
10.000
1.000.000
10.000.000
I need him to return to me in an echo:
10
1 mil
10 mil
1 milhão
10 milhões
I found and am trying to adapt a function.
<?php
echo valorPorExtenso(508600);
function valorPorExtenso($valor)
{
$rt='';
$singular = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
$plural = array("", "", "mil", "milhões", "bilhões", "trilhões","quatrilhões");
$c = array("", "100", "200", "300", "400","500", "600", "700", "800", "900");
$d = array("", "10", "20", "30", "40", "50","60", "70", "80", "90");
$d10 = array("10", "11", "12", "13", "14", "15","16", "17", "18", "19");
$u = array("", "1", "2", "3", "4", "5", "6","7", "8", "9");
$z=0;
$valor = number_format($valor, 2, ".", ".");
$inteiro = explode(".", $valor);
for($i=0;$i<count($inteiro);$i++)
for($ii=strlen($inteiro[$i]);$ii<3;$ii++)
$inteiro[$i] = "0".$inteiro[$i];
// $fim identifica onde que deve se dar junção de centenas por "e" ou por ","
$fim = count($inteiro) - ($inteiro[count($inteiro)-1] > 0 ? 1 : 2);
for ($i=0;$i<count($inteiro);$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 = count($inteiro)-1-$i;
$r .= $r ? " ".($valor > 1 ? $plural[$t] : $singular[$t]) : "";
if ($valor == "000")$z++; elseif ($z > 0) $z--;
if ($r) $rt = $rt . ((($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? ", " : " e ") : " ") . $r;
}
return($rt ? $rt : "0");
}
?>
As it has in the title, I do not want to write the number in full, I just want to add the 'thousand', 'millions', 'billions' and etc.
With the function above, this returning me
500 e 8 mil e 600
I need you to call me back
508600 mil
Another example
echo valorPorExtenso(102); //Retorna cento e 2
I need you to call me back
102
Call me back
Another example (this one working the way I need it)
tenho o número 10000000
You’re returning me (correctly)
10 milhões
You’ve already tested something?
– Sergio
I have no idea how to start trying
– sNniffer
I put one for finance if I have to, but I think your question is a duplicate of the other, if it’s purely numerical values.
– Bacco