Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Last active May 6, 2016 19:20
Show Gist options
  • Save joubertredrat/a35955b37de6f96711d840f205bfb72b to your computer and use it in GitHub Desktop.
Save joubertredrat/a35955b37de6f96711d840f205bfb72b to your computer and use it in GitHub Desktop.
number to text conception

number to text conception

The logic about number to text conversion is separate number in parts to translate to this word. Example: 329

  • 3 = hundred part
  • 2 = ten part
  • 9 = unit part

If number is teen like have sub section for this. Example: 916

  • 9 = hundred part
  • 16 = tens ten part

If section doesn't exists, will not have part. Example 206

  • 2 = hundred part
  • 0 = nothing
  • 6 = unit part

For thousands is same thing with your separated parts to works with your sufix (mil, million, billion, etc). Example: 21911

  • 2 = ten thousands part
  • 1 = unit thousands part
  • 911 = solved above
<?php
/**
* Number to word pt-br conpcetion
* @author Joubert <me+github@redrat.com.br>
*/
class NumberToWord
{
private $units = [
1 => 'um',
2 => 'dois',
3 => 'três',
4 => 'quatro',
5 => 'cinco',
6 => 'seis',
7 => 'sete',
8 => 'oito',
9 => 'nove'
];
private $tens_ten = [
10 => 'dez',
11 => 'onze',
12 => 'doze',
13 => 'treze',
14 => 'quatorze',
15 => 'quinze',
16 => 'dezesseis',
17 => 'dezessete',
18 => 'dezoito',
19 => 'dezenove'
];
private $tens = [
0 => '',
2 => 'vinte',
3 => 'trinta',
4 => 'quarenta',
5 => 'cinquenta',
6 => 'sessenta',
7 => 'setenta',
8 => 'oitenta',
9 => 'noventa'
];
private $hundred = 'ce';
private $hundred_singular = 'm';
private $hundred_plural = 'nto';
private $hundreds = [
2 => 'duzentos',
3 => 'trezentos',
4 => 'quatrocentos',
5 => 'quinhentos',
6 => 'seiscentos',
7 => 'setecentos',
8 => 'oitocentos',
9 => 'novecentos'
];
private $thousands = ['mil', 'milh', 'bilh', 'trilh', 'quatrilh', 'quintilh', 'sextilh', 'septilh'];
private $thousand_singular = 'ão';
private $thousand_plural = 'ões';
private $and = 'e';
private $number;
private $number_parts;
private $thousand_parts;
private $parts = [];
public function __construct($number)
{
$this->number = $number;
$this->number_parts = str_split($number);
$this->partNumer();
}
public function partNumer()
{
$this->partUnits();
$this->partTens();
$this->partHundreds();
$this->partThousands();
var_dump($this->parts);
}
private function partUnits()
{
if (count($this->number_parts) > 1) {
if ($this->number_parts[count($this->number_parts) - 2] != 1) {
$this->parts['units'] = $this->number_parts[count($this->number_parts) - 1];
}
} else {
$this->parts['units'] = $this->number_parts[0];
}
}
private function partTens()
{
if (count($this->number_parts) > 1) {
if ($this->number_parts[count($this->number_parts) - 2] != 1) {
$this->parts['tens'] = $this->number_parts[count($this->number_parts) - 2];
} else {
$this->parts['tens_ten'] =
$this->number_parts[count($this->number_parts) - 2].
$this->number_parts[count($this->number_parts) - 1]
;
}
}
}
private function partHundreds()
{
if (count($this->number_parts) > 2) {
$this->parts['hundreds'] = $this->number_parts[count($this->number_parts) - 3];
}
}
private function partThousands()
{
// for mil, million billion and more, reuse algorithm above
}
public function getWord()
{
$word = [];
if (isset($this->parts['hundreds'])) {
if ($this->parts['hundreds'] == 1) {
if (
(isset($this->parts['tens']) && $this->parts['tens'] > 0) ||
(isset($this->parts['units']) && $this->parts['units'] > 0) ||
isset($this->parts['tens_ten'])
) {
$word[] = $this->hundred.$this->hundred_plural;
} else {
$word[] = $this->hundred.$this->hundred_singular;
}
} else {
$word[] = $this->hundreds[$this->parts['hundreds']];
}
}
if (isset($this->parts['tens_ten'])) {
$word[] = $this->tens_ten[$this->parts['tens_ten']];
}
if (isset($this->parts['tens']) && $this->parts['tens'] > 0) {
$word[] = $this->tens[$this->parts['tens']];
}
if (isset($this->parts['units']) && $this->parts['units'] > 0) {
$word[] = $this->units[$this->parts['units']];
}
return implode(' '.$this->and.' ', $word);
}
}
<pre>
<?php
require(__DIR__.'numer-to-word.php');
$number = rand(1, 999);
$NumberToWord = new NumberToWord($number);
var_dump($number, $NumberToWord->getWord());
@joubertredrat
Copy link
Author

Hmm, probably I can move $tens_ten array data info $units

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment