Skip to content

Instantly share code, notes, and snippets.

@madgrk
Last active August 1, 2019 13:04
Show Gist options
  • Save madgrk/b9726d9607c8f4f7201804349227c167 to your computer and use it in GitHub Desktop.
Save madgrk/b9726d9607c8f4f7201804349227c167 to your computer and use it in GitHub Desktop.
Convert Money to Greek Verbal
<?php
class ConvertMoneyToString {
private $M = array('', 'ΕΝΑ ', 'ΔΥΟ ', 'ΤΡΙΑ ', 'ΤΕΣΣΕΡΑ ', 'ΠΕΝΤΕ ', 'ΕΞΙ ', 'ΕΠΤΑ ', 'ΟΚΤΩ ', 'ΕΝΝΕΑ ');
private $Mf = array('', 'ΜΙΑ ', '', 'ΤΡΕΙΣ ', 'ΤΕΣΣΕΡΙΣ ');
private $D1 = array('ΔΕΚΑ ', 'ΕΝΤΕΚΑ ', 'ΔΩΔΕΚΑ ');
private $D = array('', 'ΔΕΚΑ ', 'ΕΙΚΟΣΙ ', 'ΤΡΙΑΝΤΑ ', 'ΣΑΡΑΝΤΑ ', 'ΠΕΝΗΝΤΑ ', 'ΕΞΗΝΤΑ ', 'ΕΒΔΟΜΗΝΤΑ ', 'ΟΓΔΟΝΤΑ ', 'ΕΝΝΕΝΗΝΤΑ ');
private $E = array('', 'ΕΚΑΤΟ', 'ΔΙΑΚΟΣΙ', 'ΤΡΙΑΚΟΣΙ', 'ΤΕΤΡΑΚΟΣΙ', 'ΠΕΝΤΑΚΟΣΙ', 'ΕΞΑΚΟΣΙ', 'ΕΠΤΑΚΟΣΙ', 'ΟΚΤΑΚΟΣΙ', 'ΕΝΝΙΑΚΟΣΙ');
private $Idx = array('ΛΕΠΤΑ', 'ΕΥΡΩ', 'ΧΙΛΙΑΔΕΣ ', 'ΕΚΑΤΟΜΜΥΡΙ', 'ΔΙΣ', 'ΤΡΙΣ', 'ΤΕΤΡΑΚΙΣ ', 'ΠΕΝΤΑΚΙΣ ');
public function GetVerbal($money, $showZero = false, $showCurrency = false) {
$index = 0;
$isZero = true;
$isNegative = false;
$str = '';
if ($money < 0) {
$money = -$money;
$isNegative = true;
}
if (abs($money - floor($money)) > 0) {
$value = (int) round(100 * $money - 100 * floor($money), 0);
if ($value >= 100) {
$value -= 100;
$money += 1.0;
}
if ($value > 0) {
$isZero = false;
if ($money >= 1 && $value > 0) {
$str .= ' ΚΑΙ ';
}
$str .= $this->GetValue($value, $index, $showCurrency);
}
}
while ($money >= 1) {
$isZero = false;
$kati = $money % 1000;
$value = (int) $kati;
$money /= 1000;
$indexValue = $index;
$indexValue += 1;
$index = $indexValue;
$str = $this->GetValue($value, $index, $showCurrency) . $str;
}
if ($isZero === true) {
if ($showZero === true) {
$str = 'ΜΗΔΕΝ ';
if ($showCurrency === true) {
$str .= $this->Idx[1];
}
}
} elseif ($isNegative === true) {
$str = 'ΜΕΙΟΝ ' . $str;
}
return $str;
}
private function GetValue($money, $index, $showCurrency = false) {
if ($index == 2 && $money == 1.0) {
return "ΧΙΛΙΑ ";
}
$str = '';
$dekmon = $money % 100;
$monades = $dekmon % 10;
$ekatontades = intval($money / 100);
$dekades = intval($dekmon / 10);
if ($ekatontades == 1) {
$str = ($dekmon == 0) ? $this->E[1] . ' ' : $this->E[1] . 'Ν ';
} elseif ($ekatontades > 1) {
$str = ($index == 2) ? $this->E[$ekatontades] . 'ΕΣ ' : $this->E[$ekatontades] . 'Α ';
}
switch ($dekmon) {
case 10:
$str .= $this->D1[$monades];
break;
case 11:
$str .= $this->D1[$monades];
$monades = 0;
break;
case 12:
$str .= $this->D1[$monades];
$monades = 0;
break;
default:
$str .= $this->D[$dekades];
break;
}
if ($index == 2 && ($monades == 1 || $monades == 3 || $monades == 4)) {
$str .= $this->Mf[$monades];
} elseif ($dekmon < 10 || $dekmon > 12) {
$str .= $this->M[$monades];
}
if (strlen($str) > 0 || intval($index) == 1) {
if ($index == 0 && $money == 1) {
if ($showCurrency === true) {
$str .= ' ΛΕΠΤO';
}
} elseif ($index > 1 || $showCurrency === true) {
$str .= $this->Idx[$index];
if ($index <= 2)
return $str;
if ($index > 3)
$str .= $this->$Idx[3];
$str = ($money > 1) ? $str . 'Α ' : $str . 'Ο ';
}
}
return $str;
}
}
// Example
$obj = new ConvertMoneyToString();
for ($i = 0; $i < 5; $i += 0.01) {
print($i . ' ' . ($obj->GetVerbal($i, true, true) . "\n"));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment