Good morning, dear readers!
How are you?
Once again, I'm bringing a function that I use in my projects, and I use it whenever I need to write the value of a number in full, which isn't such an uncommon situation, right?
I added 2 optional boolean parameters
- $bolExhibirMoeda: Defines whether the function will add a reference to the currency (cents, real, etc.)
- $bolPalavraFeminina: Defines whether the function will return two hundred or two hundred, for example.
To better filter the $valor input parameter, I used the function removeFormatacaoNumero, which I already talked about here on the blog, in the post Removing formatting from a number in PHP.
As usual, I encapsulated the function in a class and created it statically, so as not to need to instantiate the class whenever I need to use the function.
<?php
class clsTexto
{
public static function valorPorExtenso( $valor = 0, $bolExibirMoeda = true, $bolPalavraFeminina = false )
{
$valor = self::removerFormatacaoNumero( $valor );
$singular = null;
$plural = null;
if ( $bolExibirMoeda )
{
$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");
}
else
{
$singular = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
$plural = array("", "", "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", "dezessete", "dezoito", "dezenove");
$u = array("", "um", "dois", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
if ( $bolPalavraFeminina )
{
if ($valor == 1)
{
$u = array("", "uma", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
}
else
{
$u = array("", "um", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
}
$c = array("", "cem", "duzentas", "trezentas", "quatrocentas","quinhentas", "seiscentas", "setecentas", "oitocentas", "novecentas");
}
$z = 0;
$valor = number_format( $valor, 2, ".", "." );
$inteiro = explode( ".", $valor );
for ( $i = 0; $i < count( $inteiro ); $i++ )
{
for ( $ii = mb_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 "," ;)
$rt = null;
$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 ( ($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;
}
$rt = mb_substr( $rt, 1 );
return($rt ? trim( $rt ) : "zero");
}
}
?>
Some examples of using the function:
//Carregamos a classe para a memória
require_once("classes/clsTexto.php");
//Vai exibir na tela "um milhão, quatrocentos e oitenta e sete mil, duzentos e cinquenta e sete e cinquenta e cinco"
echo clsTexto::valorPorExtenso("R$ 1.487.257,55", false, false);
//Vai exibir na tela "um milhão, quatrocentos e oitenta e sete mil, duzentos e cinquenta e sete reais e cinquenta e cinco centavos"
echo clsTexto::valorPorExtenso("R$ 1.487.257,55", true, false);
//Vai exibir na tela "duas mil e setecentas e oitenta e sete"
echo clsTexto::valorPorExtenso("2787", false, true);
Comentários (0)
Carregando comentários…