Good morning, dear readers!
All right with you?
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 is not such an unusual situation, is it true?
Added 2 optional boolean parameters
- $ bolView Currency: Defines if the function will add a currency reference (cents, real, etc)
- $ bolWordFeminine: Defines whether the function will return two hundred or two hundred, for example.
To better filter the input parameter $ value, I used the function removeFormatNumber, which I talked about her here on the blog, in the post Removing Number Formatting in PHP.
As usual, I encapsulated the function in a class and created it statically, so I didn't have to instantiate the class whenever I needed to use the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?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:
1 2 3 4 5 6 7 8 9 10 11 |
//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); |
Excellent code… Top!!
But… I tried to adapt to higher values and it didn't work. I complemented the text arrays with quintillion, sextillion etc and their plural versions too, but it gives an error. I would like to use up to centillion. Do you have any idea how you could complement it properly and correctly? It's for a science project.
Thanks for the help my dear, you are senior could you recommend me some courses pls?
I need this function without the real part. type 1.35 (one point thirty-five)
It is in error on line 84.
PHP Parse error: syntax error, unexpected '=' in /home/sigefonline/public_html/sigefonline/view/admin/classes/clsTexto.php on line 84
Thank you so much for sharing this code! There was a syntax error, to correct I did it like this:
In the last line, from the last for we have a ternary with this excerpt:
($ i <= $ end)
we exchange for:
($ i <= $ end)
good afternoon,
What if the number is decimal?
In the current code does it read as if it were whole? can you help?
Thank you very much, it helped me a lot.
A hug from Guinea-Bissau in Africa.
It managed to solve, because mine, is with the same error.
Thanks a lot for both functions
Thank you, Ralph. Hope this helps
Did I do something wrong?
Parse error: syntax error, unexpected '=' in C: \ xampp \ htdocs \ currency \ clsTexto.php on line 85
$ rt = $ rt. (((($ i> 0) && ($ i 0) && ($ z <1)))? (($ i <$ end)? ",": "and"): ""). $ r;
Managed to solve? If you're still having trouble, call me in the private
I have this error there.
Dirceu, thank you so much for sharing!
Please correct the “seventeen”
Eder speaks, ok? Now "seventeen" is correct. 🙂
Thanks for the remark
Hello Bobcats! How are you ?
As mentioned at the beginning of this post, this function is available in the post. https://www.dirceuresende.com/blog/removendo-formatacao-de-um-numero-no-php/
If you have any questions, do not hesitate to ask.
PHP Fatal error: Uncaught Error: Call to undefined method clsText :: removeFormatNumber ()
Thanks ! my helped here
Hello how do you correct this mistake that marceltoniote mentioned above
Vlw, friend! Your class gave a very good “advance” in my project.
Great job!
Good luck…
Thanks for the feedback, svicente99! If you have any questions, do not hesitate to ask.
Very good Dirceu, helped a lot.
Hello, thanks for making the code available. Does this exist on github?
Here's an adjustment I needed to make:
89 line: return ($ rt? Trim ($ rt): “zero”. ($ BolDisplay Currency? ”Reais and zero cents”: “”));
Orlando, good afternoon.
Thanks for the feedback. This code is not available on github, just right here.
Hug!
In mine is giving the following error:
Fatal error: Call to undefined method extended :: removeFormatNumber () in C: \ PHP Servers \ test_mdl \ extended \ extended.php on line 10
I just changed the class name to 'extensive'.
Thanks for a help in advance!
Marconi, did you change the class name too or just the file name? This error usually occurs on static objects when the class name is wrong or the method does not exist.
Good morning,
Friend I have this error:
Fatal error: Call to undefined method clsText :: removeFormatNumber () in D: \ xampp \ htdocs \ school2 \ classes \ clsText.php on line 8
Oops my fault, solved!
Alright, Marcelo. Any questions just let me know.
Guys have a very good lib that does the same thing but different. Follow the links:
github: https://github.com/malukenho/speaknumber
packages: https://packagist.org/packages/malukenho/speaknumber
NOTE: I'm not downplaying the work of the friend, I'm just giving a new alternative of a lib.
Edinaldo, thanks for visiting and for the tip of this lib. I think the post solution is simpler to implement, but this lib you shared seems to me to be more complete.
Thanks!
Max, you can change the return of the function and use the ucfirst function:
Example: return ($ rt? Ucfirst (strtolower (trim ($ rt))): "zero");
It's true ... I hadn't even remembered that. Thank you.!
Very good. What would it be like to capitalize only the first word?
Thank you my friend, helped me a lot.
Thanks for the snippet, it will be very useful to me
I couldn't use it.