Skip to content

Instantly share code, notes, and snippets.

@franciscopessoa
Last active April 27, 2020 15:43
Show Gist options
  • Save franciscopessoa/4edb4b2f72683fb5ed070c4e6a043e4e to your computer and use it in GitHub Desktop.
Save franciscopessoa/4edb4b2f72683fb5ed070c4e6a043e4e to your computer and use it in GitHub Desktop.
convert BRL to words (array) php
<?php
function calcula($valor)
{
$retorno = [];
$divisao = explode('.', $valor);
$reais = isset($divisao[0]) ? $divisao[0] : null;
$centavos = isset($divisao[1]) ? $divisao[1] : null;
if (strlen($reais) <= 6 and !is_null($reais)) {
$retorno = array_merge($retorno, calculaReais($reais));
}
if (strlen($centavos) <= 2 and !is_null($centavos)) {
$retorno = array_merge($retorno, calculaCentavos($centavos, $reais));
}
return $retorno;
}
function calculaReais($reais)
{
$retorno = [];
if (strlen($reais) == 1) {
$retorno = numeralUnidade($reais);
} else if (strlen($reais) == 2) {
$retorno = numeralDezena($reais);
} else if (strlen($reais) == 3) {
$retorno = numeralCentena($reais);
} else if (strlen($reais) == 4) {
$retorno = numeralMilhar($reais);
} else if (strlen($reais) == 5) {
$retorno = numeralMilharDezena($reais);
} else if (strlen($reais) == 6) {
$retorno = numeralMilharCentena($reais);
}
if (count($retorno) == 1 and $retorno[0] == 1) {
$retorno[] = 'real';
} else {
$retorno[] = 'reais';
}
return $retorno;
}
function calculaCentavos($centavos, $reais)
{
$retorno = [];
if (!empty($centavos) and $centavos !== '00') {
if (strlen($centavos) == 1 and $centavos != '0') {
$centavos .= '0';
}
if (!empty($reais)) {
$retorno[] = 'e';
}
if (strlen($centavos) == 1) {
$retorno = array_merge($retorno, numeralUnidade($centavos));;
} else if (strlen($centavos) == 2) {
$retorno = array_merge($retorno, numeralDezena($centavos));;
}
$retorno[] = 'centavos';
}
return $retorno;
}
function numeralUnidade(int $valor)
{
return [$valor];
}
function numeralDezena(int $valor)
{
$retorno = [];
if ($valor <= 20 or substr($valor, 1, 1) == '0') {
$retorno[] = $valor;
} else {
$retorno[] = substr($valor, 0, 1) . '0';
$retorno[] = 'e';
$retorno[] = substr($valor, 1, 1);
}
return $retorno;
}
function numeralCentena($valor)
{
$retorno = [];
if (substr($valor, 0, 1) == '0') {
$retorno = numeralDezena($valor);
} else {
if ($valor == 100) {
$retorno[] = $valor;
} else {
if (substr($valor, 0, 1) == 1) {
$retorno[] = 'cento';
} else {
$retorno[] = substr($valor, 0, 1) . '00';
}
if (substr($valor, 1, 2) != '00') {
$retorno[] = 'e';
$retorno = array_merge($retorno, numeralDezena(substr($valor, 1, 2)));
}
}
}
return $retorno;
}
function numeralMilhar(int $valor)
{
$retorno = [];
if (substr($valor, 0, 1) != '1') {
$retorno[] = substr($valor, 0, 1);
}
$retorno[] = 'mil';
if (substr($valor, 2, 2) == '00') {
$retorno[] = 'e';
}
$retorno = array_merge($retorno, numeralCentena(substr($valor, 1, 3)));
return $retorno;
}
function numeralMilharDezena(int $valor)
{
$retorno = [];
$retorno = numeralDezena(substr($valor, 0, 2));
$retorno[] = 'mil';
if (substr($valor, 3, 2) == '00') {
$retorno[] = 'e';
}
$retorno = array_merge($retorno, numeralCentena(substr($valor, 2, 3)));
return $retorno;
}
function numeralMilharCentena(int $valor)
{
$retorno = [];
$retorno = numeralCentena(substr($valor, 0, 3));
$retorno[] = 'mil';
if (substr($valor, 3, 2) == '00') {
$retorno[] = 'e';
}
$retorno = array_merge($retorno, numeralCentena(substr($valor, 2, 3)));
return $retorno;
}
$a = calcula($argv[1]);
print_r($a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment